UVa 757 - Gone Fishing

  题目大意:John有h的时间可以去钓鱼,有n湖可钓鱼,以5分钟为时间单位,每个湖初始每个单位时间可钓f条鱼,每下一个时间单位数量减少d条。同时,John只能从1号湖往后走进行钓鱼,湖之间的距离需要t个单位时间。求最多能钓到最多少鱼,并给出方案。

  贪心+暴力,从近至远依次枚举可以到达的钓鱼地点,用总时间减去中途消耗的时间,这样就可以将其当成在各个钓鱼地点之间随意转移(实际题目中给出是单方向前行的),然后在已到达的最远的钓鱼地点之内的所有钓鱼地点上按每次能钓到的鱼的最大值做贪心,若时间用不完,则剩余时间加到第一个钓鱼地点上。注意,钓鱼地点最初的钓鱼值可能为零。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 #define MAXN 30
 6 
 7 struct Lake
 8 {
 9     int id, f;
10     bool operator < (const Lake& l) const
11     {
12         if (f != l.f)   return f < l.f;
13         else return id > l.id;
14     }
15 };
16 
17 int f[MAXN], d[MAXN], t[MAXN], fish[MAXN], res[MAXN];
18 
19 int main()
20 {
21 #ifdef LOCAL
22     freopen("in", "r", stdin);
23 #endif
24     int n;
25     bool first = true;
26     while (scanf("%d", &n) && n)
27     {
28         int h;
29         scanf("%d", &h);
30         for (int i = 0; i < n; i++)
31             scanf("%d", &f[i]);
32         for (int i = 0; i < n; i++)
33             scanf("%d", &d[i]);
34         t[0] = 0;
35         for (int i = 1; i < n; i++)
36         {
37             scanf("%d", &t[i]);
38             t[i] += t[i-1];
39         }
40         int ans = -1;
41         for (int i = 0; i < n; i++)  // the last lake arrived
42         {
43             int time = h * 12;
44             if (t[i] > time)   break;
45             time -= t[i];
46             priority_queue<Lake> q;
47             Lake tmp;
48             for (int j = 0; j <= i; j++)
49                 if (f[j])
50                     q.push( (Lake){j, f[j]} );
51             memset(fish, 0, sizeof(fish));
52             int sum = 0;
53             while (!q.empty() && time > 0)
54             {
55                 tmp = q.top();
56                 q.pop();
57                 sum += tmp.f;
58                 int id = tmp.id;
59                 fish[id]++;
60                 time--;
61                 tmp.f -= d[id];
62                 if (tmp.f > 0)   q.push(tmp);
63             }
64             fish[0] += time;
65             if (sum > ans)  
66             {
67                 ans = sum;
68                 memcpy(res, fish, sizeof(fish));
69             }
70         }
71         if (first)  first = false;
72         else  printf("\n");
73         for (int i = 0; i < n; i++)
74             printf("%d%s", res[i]*5, (i==n-1) ? "\n" : ", ");
75         printf("Number of fish expected: %d\n", ans);
76     }
77     return 0;
78 }
79             
View Code

 

posted @ 2013-08-18 16:56  xiaobaibuhei  阅读(314)  评论(0编辑  收藏  举报