Codeforces 746D:Green and Black Tea(乱搞)

http://codeforces.com/contest/746/problem/D

题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO。

思路:首先,设x = min(a,b),y = max(a,b),然后如果(y + x)/(x + 1) > k,那么就输出NO。即把 y 平均分成 x + 1 份,向上取整。然后开始搞。。。

搞的时候我直接对于 y 每一次都输出最大的份数了,导致后面的 x 过多,还自以为是平均的。

所以应该处理一下,如果有剩余的话,那么剩余的数目是 cnt = y % (x + 1),就是在 a + 1 组 y 里面,有 cnt 组是要多输出一个的,其他的只要输出 y / (x + 1)个就可以了。

细心细心!!!

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 #include <queue>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 using namespace std;
12 #define INF 0x3f3f3f3f
13 #define N 200010
14 typedef long long LL;
15 
16 char s[N];
17 
18 int main() {
19     int n, k, a, b;
20     cin >> n >> k >> a >> b;
21     char c = 'G', d = 'B';
22     if(a >= b) {
23         swap(a, b);
24         swap(c, d);
25     }
26     int x = b / (a + 1);
27     int y = x + (b % (a + 1) ? 1 : 0);
28     if(y > k) puts("NO");
29     else {
30         int j = 0;
31         for(int i = 0; i < a + 1; i++) {
32             if(i) putchar(c);
33             int cnt = x + ((i < b % (a + 1)) ? 1 : 0);
34             for(int q = 0; q < cnt; q++) putchar(d);
35         }
36         puts("");
37     }
38     return 0;
39 }
原文地址:https://www.cnblogs.com/fightfordream/p/6196030.html