[关闭]
@Dmaxiya 2018-08-17T02:19:15.000000Z 字数 4452 阅读 1135

Codeforces Round #460 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #460 (Div. 2)
过题数:3
排名:2001/13616

A. Supermarket

题意

个超市,每个超市里的苹果价格为 元每 千克,现在要买 千克的苹果,问最少需要花费多少元。

输入

第一行为两个整数 ,接下去 行每行两个整数

输出

输出要买 千克苹果的最小花费,与标准答案误差在 内都认为程序是正确的。

样例

输入 输出 提示
3 5
1 2
3 4
1 3
1.66666667 最优的选择是在第 个超市买 千克苹果,总花费为 元。
2 1
99 100
98 99
0.98989899 最优的选择是在第 个超市买 千克苹果,总花费为 元。

题解

对每一个超市计算一次 千克苹果的总价,取最小值。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. int n, m;
  19. double a, b;
  20. int main() {
  21. #ifdef Dmaxiya
  22. freopen("test.txt", "r", stdin);
  23. #endif // Dmaxiya
  24. ios::sync_with_stdio(false);
  25. while(scanf("%d%d", &n, &m) != EOF) {
  26. double ans = INT_MAX;
  27. for(int i = 0; i < n; ++i) {
  28. scanf("%lf%lf", &a, &b);
  29. ans = min(ans, a / b * m);
  30. }
  31. printf("%.10f\n", ans);
  32. }
  33. return 0;
  34. }

B. Perfect Number

题意

如果一个数字各个位上的数字和等于 ,那么这个数字就是“完美数”,现在给定数字 ,求第 小的“完美数”。

输入

输入只包含一个整数

输出

输出第 小的“完美数”。

样例

输入 输出
1 19
2 28

题解

打表预处理,打表到 可以得到最小的 个答案。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 100000 + 100;
  19. int n, cnt;
  20. int ans[maxn];
  21. bool judge(int x) {
  22. int tmp = 0;
  23. while(x != 0) {
  24. tmp += x % 10;
  25. x /= 10;
  26. }
  27. return tmp == 10;
  28. }
  29. int main() {
  30. #ifdef Dmaxiya
  31. freopen("test.txt", "r", stdin);
  32. #endif // Dmaxiya
  33. ios::sync_with_stdio(false);
  34. cnt = 1;
  35. for(int i = 1; i <= 20000000; ++i) {
  36. if(judge(i)) {
  37. ans[cnt++] = i;
  38. }
  39. }
  40. while(scanf("%d", &n) != EOF) {
  41. printf("%d\n", ans[n]);
  42. }
  43. return 0;
  44. }

C. Seat Arrangements

题意

在一个 的字符矩阵里,问在同一行或者同一列内总共能找到多少个连续的 .

输入

第一行为三个整数 ,接下去 行每行 个字符,每个字符只可能是 . 或者 *

输出

输出答案。

样例

输入 输出 提示
2 3 2
**.
...
3 可以找到以下三种连续的 .
1 2 2
..
1
3 3 4
.*.
*.*
.*.
0

题解

直接按照题意横着、竖着各扫一次,注意特判 的情况。

过题代码

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

D. Substring

题意

在一个 个节点 条边的有向图上,每个节点上都有一个小写字母,定义一条路径的权值为这条路径上所有节点的字符出现次数的最大值,输出图上所有路径的最大权值。

输入

第一行为两个整数 ,第二行为一个字符串 ,第 个小写字母表示第 个节点上的字符,接下去 行每行两个整数 ,表示从节点 到节点 有一条边。给出的图可能有自环、重边。

输出

如果最大权值为无穷大,则输出 ,否则输出最大权值。

样例

输入 输出 提示
5 4
abaca
1 2
1 3
3 4
4 5
3 最大权值的路径为 ,因为这条路径上出现了 个 'a',所以其权值为
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
-1
10 14
xzyzyzyzqx
1 2
2 4
3 5
4 5
2 6
6 8
6 5
2 10
3 9
10 9
4 6
1 10
2 8
3 7
4

题解

如果图上存在环则输出 ,用拓扑排序判环,用一个 数组记录到达第 个节点时,字符 出现的最大次数, 的转移可以在拓扑排序的同时进行。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 300000 + 100;
  19. int n, m, u, v, ans;
  20. char str[maxn];
  21. int deg[maxn], dp[maxn][26];
  22. vector<int> G[maxn];
  23. queue<int> que;
  24. int id(char ch) {
  25. return ch - 'a';
  26. }
  27. bool topsort() {
  28. for(int i = 1; i <= n; ++i) {
  29. if(deg[i] == 0) {
  30. que.push(i);
  31. int w = id(str[i]);
  32. ++dp[i][w];
  33. }
  34. }
  35. while(!que.empty()) {
  36. int tmp = que.front();
  37. que.pop();
  38. int len = G[tmp].size();
  39. for(int i = 0; i < len; ++i) {
  40. int pos = G[tmp][i];
  41. --deg[pos];
  42. if(deg[pos] == 0) {
  43. que.push(pos);
  44. }
  45. for(int j = 0; j < 26; ++j) {
  46. int d = (id(str[pos]) == j);
  47. dp[pos][j] = max(dp[pos][j], dp[tmp][j] + d);
  48. }
  49. }
  50. }
  51. for(int i = 1; i <= n; ++i) {
  52. if(deg[i] != 0) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. int main() {
  59. #ifdef Dmaxiya
  60. freopen("test.txt", "r", stdin);
  61. #endif // Dmaxiya
  62. ios::sync_with_stdio(false);
  63. while(scanf("%d%d", &n, &m) != EOF){
  64. ans = 0;
  65. for(int i = 1; i <= n; ++i) {
  66. G[i].clear();
  67. deg[i] = 0;
  68. for(int j = 0; j < 26; ++j) {
  69. dp[i][j] = 0;
  70. }
  71. }
  72. scanf("%s", str + 1);
  73. for(int i = 0; i < m; ++i) {
  74. scanf("%d%d", &u, &v);
  75. G[u].push_back(v);
  76. ++deg[v];
  77. }
  78. if(topsort()) {
  79. printf("-1\n");
  80. continue;
  81. }
  82. for(int i = 1; i <= n; ++i) {
  83. for(int j = 0; j < 26; ++j) {
  84. ans = max(ans, dp[i][j]);
  85. }
  86. }
  87. printf("%d\n", ans);
  88. }
  89. return 0;
  90. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注