贪心 Codeforces Round #287 (Div. 2) A. Amr and Music

题目传送门

 1 /*
 2     贪心水题
 3 */
 4 #include <cstdio>
 5 #include <algorithm>
 6 #include <iostream>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <vector>
10 #include <set>
11 #include <map>
12 #include <string>
13 using namespace std;
14 
15 const int MAXN = 1e2 + 10;
16 const int INF = 0x3f3f3f3f;
17 struct NODE
18 {
19     int num, id;
20 }node[MAXN];
21 
22 bool cmp(NODE a, NODE b)
23 {
24     return a.num < b.num;
25 }
26 
27 void work(int n, int k)
28 {
29     int x = 1;    int sum = 0;
30     for (int i=1; i<=n; ++i)
31     {
32         sum += node[i].num;        x = i;
33         if (sum > k)
34         {
35             x -= 1;        break;
36         }
37     }
38 
39     printf ("%d
", x);
40     for (int i=1; i<=x; ++i)
41     {
42         printf ("%d%c", node[i].id, (i==x) ? '
' : ' ');
43     }
44         
45 }
46 
47 int main(void)
48 {
49     #ifndef ONLINE_JUDGE
50     freopen ("A.in", "r", stdin);
51     #endif
52 
53     int n, k;
54     while (~scanf ("%d%d", &n, &k))
55     {
56         for (int i=1; i<=n; ++i)
57         {
58             scanf ("%d", &node[i].num);
59             node[i].id = i;
60         }
61 
62         sort (node+1, node+1+n, cmp);
63 
64         work (n, k);
65     }
66 
67 
68     return 0;
69 }
编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4366592.html