• JZOJ 5346. 【NOIP2017提高A组模拟9.5】NYG的背包


    题目

    分析

    很神奇的贪心

    (Code)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    
    const int N = 100005;
    int n , T , cnt1 , cnt2;
    LL v;
    struct node{
    	int a , b;
    }p[N] , q[N];
    
    bool cmp1(node x , node y){return x.a < y.a;}
    bool cmp2(node x , node y){return x.b < y.b;}
    
    int main()
    {
    	freopen("backpack.in" , "r" , stdin);
    	freopen("backpack.out" , "w" , stdout);
    	scanf("%d" , &T);
    	while (T--)
    	{
    		scanf("%d%lld" , &n , &v);
    		cnt1 = cnt2 = 0;
    		int fl = 1 , a , b;
    		for(register int i = 1; i <= n; i++)
    		{
    			scanf("%d%d" , &a , &b);
    			if (a <= b) p[++cnt1] = node{a , b};
    			else q[++cnt2] = node{a , b};
    		}
    		sort(p + 1 , p + cnt1 + 1 , cmp1);
    		for(register int i = 1; i <= cnt1; i++)
    		{
    			a = p[i].a , b = p[i].b;
    			if (v < a){fl = 0; break;}
    			v = v - a + b;
    		}
    		if (fl == 0){printf("No
    "); continue;}
    		sort(q + 1 , q + cnt2 + 1 , cmp2);
    		for(register int i = 1; i <= cnt2; i++) v = v - q[i].a + q[i].b;
    		for(register int i = 1; i <= cnt2; i++)
    		{
    			a = q[i].a , b = q[i].b;
    			if (v < b){fl = 0; break;}
    			v = v - b + a;
    		}
    		if (fl == 0){printf("No
    "); continue;}
    		printf("Yes
    ");
    	}
    }
    
  • 相关阅读:
    uva10285 Longest Run on a Snowboard(DP)
    typecho 0.8 营销引擎
    新浪博客营销插件
    忍者X3备份说明
    QQ空间、说说抓取引擎
    yiqicms发布插件的使用
    SHOPEX v4.85 发布插件
    ecshop2.73插件使用帮助
    Destoon V5 发布插件
    Wordpress3.52营销引擎
  • 原文地址:https://www.cnblogs.com/leiyuanze/p/13785140.html
Copyright © 2020-2023  润新知