题目链接:https://code.google.com/codejam/contest/4224486/

 

Problem A. Mushroom Monster

这题题意就是,有N个时间点,每个时间点,Kaylin可以吃掉一定数量的mushroom,Bartholomew可以放入任意数量的mushroom。

现在给出N个时间点分别有多少mushroom。

问:若Kaylin每个时间点可以吃任意数量的mushroom,那为了配合每个时间点的mushroom,Kaylin最少要吃掉多少蘑菇。

问:若Kaylin已恒定速度吃mushroom(即在每个时间点间吃的数量相同,若盘子空了则暂停进食),那为了配合每个时间点的mushroom,Kaylin最少要吃掉多少蘑菇。

 

看懂题目就是水题,第一问,只要吃掉下一个时间点相对于当前时间点减少的mushroom数量。

第二问,只要保证吃的速度要大于等于所有时间点mushroom减少的数量,即取需求速度最大值。

 

代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <numeric>
 6 #include <queue>
 7 using namespace std;
 8 
 9 const int MAXN = 1010;
10 const int INF = 0x3f3f3f3f;
11 
12 int a[MAXN], maxr[MAXN];
13 int T, n;
14 
15 int solve1() {
16     int res = 0;
17     for(int i = 0; i < n - 1; ++i)
18         res += max(0, a[i] - a[i + 1]);
19     return res;
20 }
21 
22 int solve2() {
23     int spd = 0;
24     for(int i = 0; i < n - 1; ++i)
25         spd = max(spd, a[i] - a[i + 1]);
26     int res = 0;
27     for(int i = 0; i < n - 1; ++i)
28         res += min(a[i], spd);
29     return res;
30 }
31 
32 int main() {
33     freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
34     scanf("%d", &T);
35     for(int t = 1; t <= T; ++t) {
36         scanf("%d", &n);
37         for(int i = 0; i < n; ++i)
38             scanf("%d", &a[i]);
39         maxr[n] = 0;
40         for(int i = n - 1; i >= 0; --i)
41             maxr[i] = max(maxr[i + 1], a[i]);
42         printf("Case #%d: %d %d\n", t, solve1(), solve2());
43     }
44 }
View Code

相关文章:

  • 2020-04-12
  • 2022-02-09
  • 2022-01-11
  • 2022-12-23
  • 2021-05-22
  • 2021-07-11
  • 2021-06-11
  • 2022-02-06
猜你喜欢
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2021-08-24
  • 2022-12-23
相关资源
相似解决方案