1351 topcoder 吃点心

https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351

先按low从大到小贪心再high从小到大贪心

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <sstream>
 6 #include <string>
 7 #include <algorithm>
 8 #include <list>
 9 #include <map>
10 #include <vector>
11 #include <queue>
12 #include <stack>
13 #include <cmath>
14 #include <cstdlib>
15 // #include <conio.h>
16 using namespace std;
17 #define clc(a,b) memset(a,b,sizeof(a))
18 #define inf 0x3f3f3f3f
19 #define lson l,mid,rt<<1
20 #define rson mid+1,r,rt<<1|1
21 const int N=100010;
22 const int MOD = 1e9+7;
23 #define LL long long
24 double const pi = acos(-1);
25 void fre() {
26     freopen("in.txt","r",stdin);
27 }
28 // inline int r() {
29 //     int x=0,f=1;char ch=getchar();
30 //     while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
31 //     while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
32 // }
33 struct  node{
34      int x,y;    
35 }p[55];
36 
37 bool cmp1(const node &a,const node &b){
38      return a.x>b.x;
39 }
40 
41 bool cmp2(const node &a,const node &b){
42      return a.y<b.y;
43 }
44 
45 int main(){
46     // fre();
47     int T;
48     scanf("%d",&T);
49     while(T--){
50          int n,c,x;
51          scanf("%d%d%d",&n,&c,&x);
52          for(int i=0;i<n;i++){
53              scanf("%d%d",&p[i].x,&p[i].y);
54          } 
55          sort(p,p+n,cmp1);
56          int sum=0,ans1=0;
57          for(int i=0;i<n;i++){
58              if(sum<x)
59                  ans1++,sum+=p[i].x;
60              else
61                 break;
62          }
63          int ans2=n;
64          sort(p,p+n,cmp2);
65          for(int i=0;i<n;i++){
66              if(c-p[i].y>=x){
67                 ans2--,c-=p[i].y;
68              }
69              else
70                 break;
71          }
72          printf("%d\n",min(ans1,ans2));
73     }
74     return 0;
75 }

 

posted @ 2016-06-25 00:14  yyblues  阅读(188)  评论(0编辑  收藏  举报