UVA 10026 Shoemaker's Problem

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #define sc(x)    scanf("%d",&x)
 5 #define sc2(x,y)     scanf("%d%d", &x, &y)
 6 #define pf(x)    printf("%d\n",x)
 7 #define PF(x)    printf("%d ",x)
 8 #define P    printf("\n")
 9 #define FOR(i,b,e)    for(int i=b;i<N;i++)
10 using namespace std;
11 const int MAX = 1005;
12 struct node
13 {
14     int t, f;
15     double v;
16     int num;
17 } job[MAX];
18 int cmp(const node &a, const node &b)
19 {
20     return a.v > b.v;
21 }
22 int main()
23 {
24     int N, T;
25     sc(T);
26     while(T--)
27     {
28         sc(N);
29         FOR(i,0,N)
30         {
31             sc2(job[i].t, job[i].f);
32             job[i].v = job[i].f * 1.0 / job[i].t;
33             job[i].num = i + 1;
34         }
35         sort(job, job+N, cmp);
36         FOR(j,0,N)
37         if(j < N-1)
38             PF(job[j].num);
39         else
40             pf(job[j].num);
41         if(T)
42             P;
43     }
44     return 0;
45 }
View Code

就是背包问题,使用贪心法,求得价值最高的即可

使用中注意结构体定义之后的比较应该如何使用,同时注意结构体数组的定义

 

百度中:定义vector,vector<node> v,也是属于数组中的用法,vector属于动态数组,不容易溢出

posted @ 2015-04-04 23:06  PastLIFE  阅读(121)  评论(0编辑  收藏  举报