zoukankan      html  css  js  c++  java
  • 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
    ");
    	}
    }
    
  • 相关阅读:
    topK问题 前K个高频元素 leetcode692
    反转链表 leetcode206
    关于IO多路复用的简单整理
    两数之和 leetcode1
    使用 jenkins 发布 前端 项目
    CentOS7 部署 nacos 集群
    JWT
    keepalived 的 unicast 单播模式
    使用 keepalived 高可用 nginx
    翻转二叉树 leetcode226
  • 原文地址:https://www.cnblogs.com/leiyuanze/p/13785140.html
Copyright © 2011-2022 走看看