CodeForces 507A Amr and Music (贪心)
http://codeforces.com/contest/507/problem/A
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct Num { int v; int id; }; Num num[100+10]; int cmp1(Num a,Num b) { return a.v<b.v; } int cmp2(Num a,Num b) { return a.id<b.id; } int main() { int n,k; int i,j; while(scanf("%d%d",&n,&k)!=EOF) { int ans=0; int tot=k; for(i=1;i<=n;i++) { scanf("%d",&num[i].v); num[i].id=i; } sort(num+1,num+n+1,cmp1); for(i=1;i<=n;i++) { if(tot-num[i].v<0) break; tot-=num[i].v; ans++; } if(ans==0) printf("0\n"); else { printf("%d\n",ans); sort(num+1,num+ans+1,cmp2); for(i=1;i<=ans;i++) { printf("%d",num[i].id); if(i==ans) printf("\n"); else printf(" "); } } } }