贪心之最优装载

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <algorithm>
 5 #include <stack>
 6 #include <queue>
 7 using namespace std;
 8 
 9 const int maxn=100;
10 
11 struct node
12 {
13     int w;
14     int num;
15 }nodes[maxn];
16 bool cmp(node m,node n)
17 {
18     return m.w<n.w;
19 }
20 int main()
21 {
22     int c;
23     int n;
24     //cout<<"C="<<endl;
25     cin>>c;
26     //cout<<"n="<<endl;
27     cin>>n;
28     for(int i=0;i<n;i++)
29     {
30         cin>>nodes[i].w;
31         nodes[i].num=i;
32     }
33     int x[maxn];
34     memset(x,0,sizeof(x));
35     sort(nodes,nodes+n,cmp);
36     for(int i=0;i<n&&nodes[i].w<=c;i++)
37     {
38         x[nodes[i].num]=1;
39         c-=nodes[i].w;
40     }
41     for(int i=0;i<n;i++)
42         if(x[nodes[i].num]==1)
43             cout<<nodes[i].num<<" ";
44     cout<<endl;
45 }
46 /*
47  10
48  5
49  4 5 3 2 6
50  */

 输入示例:

10

 5

 4 5 3 2 6

输出示例:

The ans is

  The 3th   The 2th   The 0th 

 

猜你喜欢

转载自www.cnblogs.com/wangxuelin/p/9021119.html