SCU's closet(排序法的应用)

来源:互联网 发布:尺八制作数据 编辑:程序博客网 时间:2024/06/06 01:54

http://cstest.scu.edu.cn/soj/problem.action?id=4263

B: SCU's closet

Description

Summer comes with the hot weather.Today xiaoL wants to take the heavy coats in her closet out and bring her T-shirts and skirts in.We have konwn that the volume of SCU's closet is V units,xiaoL has N pieces of clothing to put in,each of them needs Ai units of space.When putting the i-th clothes into the closet,she finds that she needs Bi units of space to ensure the i-th clothes will be placed well.After that,the volume of the closet will decrease by Ai.Now,She wonders whether she can move all her clothes into the closet.

Input

There are multiple test cases,for each case,the first line contains 2 integers:V,N,then followed N lines,each of them contains 2 integers Ai and Bi as descriped above.

( 0 < V < 10000, 0 < N<  1000, 0 < Ai < V, Ai <= Bi < 1000 )

Output

For each test case output "YES" if she can put all her clothes into the closet or else output "NO"

Sample Input

20 3

10 20

3 10

1 7

10 2

1 10

2 11

Sample Output

YES

NO

Author

lentty 

题意:给你一个衣橱体积为V

每件衣服的体积为ai,总共需要bi的空间,问是否可以把全部衣服都放进去。

这里我原来的题意完全理解错了,导致一直解不出来

思路:排序,贪心。

两种择优情况:一。按所空间较大优先来考虑

 二。按所体积较小的考虑

 思路:按照以上两种情况进行排序:只要有一种满足情况即可

*/

#include<stdio.h>#include<algorithm>#include <iostream>using namespace std;const int maxn=1000+10;struct cloth{int a;int b;}c1[maxn],c2[maxn];int cmp1(cloth ch1,cloth ch2 ) //按所空间较大优先来考虑{           return (ch1.b>ch2.b)||(ch1.b==ch2.b&&ch1.a<ch2.a);}int cmp2(cloth ch1,cloth ch2 )  //按所体积较小的考虑{           return (ch1.a<ch2.a)||(ch1.a==ch2.a&&ch1.b>ch2.b);}int main(){  int n,v,i,ok1,ok2,t1,t2;while(scanf("%d%d",&v,&n)!=EOF) {             ok1=1;              ok2=1;              t1=v;              t2=v;           for(i=0;i<n;i++)          {           scanf("%d%d",&c1[i].a,&c1[i].b);           c2[i].a=c1[i].a;           c2[i].b=c1[i].b;           }           sort(c1,c1+n,cmp1);           sort(c2,c2+n,cmp2);           //for(i=0;i<n;i++)           //printf("%d %d\n",c[i].a,c[i].b);           for(i=0;i<n;i++)           {if(t1<c1[i].b)              {ok1=0;              break;              }              t1=t1-c1[i].a;           }           for(i=0;i<n;i++)           {if(t2<c2[i].b)              {ok2=0;              break;              }              t2=t2-c2[i].a;           }           if(ok1==1||ok2==1)           printf("YES\n");           else           printf("NO\n");}return 0;}