[关闭]
@Dmaxiya 2018-08-17T10:25:11.000000Z 字数 6611 阅读 1143

Codeforces Round #491 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #491 (Div. 2)
过题数:5
排名:384/7193

A. If at first you don't succeed...

题意

个人去第一家餐厅, 个人去第二家餐厅, 个人两家餐厅都去,总共有 个人,已经知道至少有一个人两家餐厅都不去,问 是否是合法的,如果不合法,输出 ,否则输出有多少个人两家餐厅都不去。

输入

输入为四个整数

输出

输出有多少个人两家餐厅都没去,如果输入的数字是不合法的,就输出 /。

样例

输入 输出 提示
10 10 5 20 5 符合这组数据的情况是: 个人只去第一家餐厅, 个人只去第二家餐厅, 个人两家餐厅
都去了, 个人两家餐厅都没去。
2 2 0 4 -1 假设有 个人只去第一家餐厅, 个人只去第二家餐厅,没有人两家餐厅都去,总共只有
个人,说明没有人两家餐厅都不去,与题意不符。
2 2 2 1 -1 去餐厅的人数比总人数多,这是不可能出现的情况。

题解

首先应该满足 ,在满足该条件的情况下,可以由 计算出两家餐厅都不去的人数,这个人数应该不小于

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. typedef long long LL;
  20. int A, B, C, N;
  21. int main() {
  22. #ifdef Dmaxiya
  23. freopen("test.txt", "r", stdin);
  24. // freopen("test1.out", "w", stdout);
  25. #endif // Dmaxiya
  26. ios::sync_with_stdio(false);
  27. while(scanf("%d%d%d%d", &A, &B, &C, &N) != EOF) {
  28. if(C > min(A, B)) {
  29. printf("-1\n");
  30. continue;
  31. }
  32. int tmp = (A + B) - C;
  33. tmp = N - tmp;
  34. if(tmp < 1) {
  35. printf("-1\n");
  36. } else {
  37. printf("%d\n", tmp);
  38. }
  39. }
  40. return 0;
  41. }

B. Getting an A

题意

给定 个分数,分数只有 四种, 可以修改其中的一些分数,问他最少修改多少个分数,可以使得这些分数的平均分等于 ,平均分的计算方式为所有分数的和除以 之后四舍五入到整数位。

输入

第一行为一个整数 ,第二行包含 个整数,每个整数都在区间 内。

输出

输出应该修改的最少的分数个数。

样例

输入 输出 提示
3
4 4 4
2 只要修改其中的两个 ,就可以使平均分为
4
5 4 5 5
0 的平均分已经达到了 ,所以不用修改任何分数。
4
5 3 3 5
1 可以将其中一个 修改为 ,这样他的平均分就可以达到

题解

贪心地从最低的分数开始修改,因为修改最低分才能使总分提高得最多,只要使总分达到 ,就能使平均分达到

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. typedef long long LL;
  20. const int maxn = 100 + 100;
  21. int n;
  22. int num[maxn];
  23. int main() {
  24. #ifdef Dmaxiya
  25. freopen("test.txt", "r", stdin);
  26. // freopen("test1.out", "w", stdout);
  27. #endif // Dmaxiya
  28. ios::sync_with_stdio(false);
  29. while(scanf("%d", &n) != EOF) {
  30. int ans = 0;
  31. double sum = 0;
  32. double tot = n * 4.5;
  33. for(int i = 0; i < n; ++i) {
  34. scanf("%d", &num[i]);
  35. sum += num[i];
  36. }
  37. sort(num, num + n);
  38. for(int i = 0; i < n; ++i) {
  39. if(sum >= tot) {
  40. break;
  41. }
  42. sum += 5 - num[i];
  43. ++ans;
  44. }
  45. printf("%d\n", ans);
  46. }
  47. return 0;
  48. }

C. Candies

题意

最初有 个糖果, 分这些糖果, 每个白天吃 个糖果, 晚上吃剩下的糖果中的 (向下取整,比如 等于 等于 )个糖果,如果糖果没有吃完,第二天、第三天继续……直到吃完糖果,如果剩下的糖果数量少于 个,那么 就会把剩下的糖果全部吃完,问 每天最少要吃多少个,才能使他吃的糖果数量至少为

输入

输入只有一个整数

输出

输出 每天至少要吃的糖果数

样例

输入 输出 提示
68 3 时,糖果数量的变化为:



此时 可以吃到 个糖果而 只能吃到 个。

题解

由于答案 满足二分性质( 越大 可以吃到的糖果数量越多),所以可以通过二分 来得到答案。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. typedef long long LL;
  20. LL n;
  21. bool judge(LL k, LL nn) {
  22. LL a, b;
  23. a = b = 0;
  24. while(nn != 0) {
  25. if(nn >= k) {
  26. a += k;
  27. nn -= k;
  28. } else {
  29. a += nn;
  30. nn = 0;
  31. }
  32. LL tmp = nn / 10;
  33. b += tmp;
  34. nn -= tmp;
  35. }
  36. return a >= b;
  37. }
  38. int main() {
  39. #ifdef Dmaxiya
  40. freopen("test.txt", "r", stdin);
  41. // freopen("test1.out", "w", stdout);
  42. #endif // Dmaxiya
  43. ios::sync_with_stdio(false);
  44. while(scanf("%I64d", &n) != EOF) {
  45. LL high = n;
  46. LL low = 0;
  47. LL mid;
  48. while(high - low > 1) {
  49. mid = (high + low) >> 1;
  50. if(judge(mid, n)) {
  51. high = mid;
  52. } else {
  53. low = mid;
  54. }
  55. }
  56. printf("%I64d\n", high);
  57. }
  58. return 0;
  59. }

D. Bishwock

题意

有以下四种方块:
XX XX .X X.
X. .X XX XX
'X' 表示方块中有砖块,'.' 表示方块中没有东西填充,现在给出两行方块,'0'(零)表示空 'X' 表示有砖块填充,问在不破坏这两行方块中有砖块的地方的情况下,最多能放下这种方块多少块。

输入

输入为两行长度相同的字符串,字符串内只包含字符 '0''X',字符串长度最大为

输出

输出能放下的数量最多的方块。

样例

输入 输出
00
00
1
00X00X0XXX0
0XXX0X00X00
4
0X0X0
0X0X0
0
0XXX0
00000
2

题解

贪心填充方块,能填则填,统计每一列有多少个 '0',如果这一列有 '0',就看下一列是否有 '0',如果有,就把这一列的 '0' 的个数 ,将下一列的 '0' 的个数 ,如果这一列有 '0',就看下一列的 '0' 的个数是否不少于 个,如果是,就把这一列的 '0' 的个数 ,下一列的 '0' 的个数

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. typedef long long LL;
  20. const int maxn = 200;
  21. char str[2][maxn];
  22. int cnt[maxn];
  23. int main() {
  24. #ifdef Dmaxiya
  25. freopen("test.txt", "r", stdin);
  26. // freopen("test1.out", "w", stdout);
  27. #endif // Dmaxiya
  28. ios::sync_with_stdio(false);
  29. while(scanf("%s%s", str[0], str[1]) != EOF) {
  30. memset(cnt, 0, sizeof(cnt));
  31. int len = strlen(str[0]);
  32. for(int i = 0; i < len; ++i) {
  33. if(str[0][i] == '0') {
  34. ++cnt[i];
  35. }
  36. if(str[1][i] == '0') {
  37. ++cnt[i];
  38. }
  39. }
  40. int ans = 0;
  41. for(int i = 0; i < len - 1; ++i) {
  42. if(cnt[i] == 1) {
  43. if(cnt[i + 1] == 2) {
  44. cnt[i] -= 1;
  45. cnt[i + 1] -= 2;
  46. ++ans;
  47. }
  48. } else if(cnt[i] == 2) {
  49. if(cnt[i + 1] >= 1) {
  50. cnt[i] -= 2;
  51. cnt[i + 1] -= 1;
  52. ++ans;
  53. }
  54. }
  55. }
  56. printf("%d\n", ans);
  57. }
  58. return 0;
  59. }

E. Bus Number

题意

给定一个数字 ,将 的每一位数字打散后重组,问有多少种合法的方式。合法的重组方式为:如果数字 中出现了某个数位 ,那么重组后的数字中必须至少出现一次 ,且重组后的数字不应该有前导零。

输入

输入只有一个整数

输出

输出所有合法的重组方案数。

样例

输入 输出 提示
97 2 合法的重组方式只有:
2028 13 以下数字都是合法的重组方式:

题解

先统计 的每个数位上数字 出现的次数 ,然后 枚举每个数字出现的次数,如果 ,则直接跳过,否则从 开始枚举 出现的次数,对于每种数字出现的分配方式,设 ,先计算所有非 数字的组合方案数,为 ,最后乘上 在其中合法的重组方案数:,把所有数字出现的分配方案数加到答案里,就是最终答案。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. typedef long long LL;
  20. const int maxn = 19;
  21. LL ans, n;
  22. int cnt[maxn], ccnt[maxn];
  23. LL C[maxn][maxn];
  24. void dfs(int depth) {
  25. if(depth == 10) {
  26. LL tmp = 1;
  27. int dig = 0;
  28. for(int i = 1; i <= 9; ++i) {
  29. if(ccnt[i] == 0) {
  30. continue;
  31. }
  32. tmp *= C[dig + ccnt[i]][ccnt[i]];
  33. dig += ccnt[i];
  34. }
  35. if(ccnt[0] != 0) {
  36. tmp *= C[dig + ccnt[0] - 1][ccnt[0]];
  37. }
  38. ans += tmp;
  39. return ;
  40. }
  41. if(cnt[depth] != 0) {
  42. for(int i = 1; i <= cnt[depth]; ++i) {
  43. ccnt[depth] = i;
  44. dfs(depth + 1);
  45. }
  46. } else {
  47. ccnt[depth] = 0;
  48. dfs(depth + 1);
  49. }
  50. }
  51. int main() {
  52. #ifdef Dmaxiya
  53. freopen("test.txt", "r", stdin);
  54. // freopen("test1.out", "w", stdout);
  55. #endif // Dmaxiya
  56. ios::sync_with_stdio(false);
  57. for(int i = 0; i < maxn; ++i) {
  58. for(int j = 0; j < maxn; ++j) {
  59. if(j == 0 || j == i) {
  60. C[i][j] = 1;
  61. } else {
  62. C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  63. }
  64. }
  65. }
  66. while(scanf("%I64d", &n) != EOF) {
  67. ans = 0;
  68. memset(cnt, 0, sizeof(cnt));
  69. LL nn = n;
  70. while(nn != 0) {
  71. ++cnt[nn % 10];
  72. nn /= 10;
  73. }
  74. dfs(0);
  75. printf("%I64d\n", ans);
  76. }
  77. return 0;
  78. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注