[关闭]
@Dmaxiya 2018-08-17T10:15:18.000000Z 字数 5557 阅读 869

Educational Codeforces Round 42

Codeforces


Contests 链接:Educational Codeforces Round 42
过题数:5
排名:103/11738

A. Equator

题意

总共要刷 天的题,第 天他刷了 道题,他想要在刷题量达到总量的一半的那天晚上庆祝一下,问他将在第几天庆祝。

输入

第一行为一个整数 ,第二行包含 个整数

输出

输出他将在第几天庆祝,天数从 开始计算。

样例

输入
4
1 3 2 1
输出
2
提示
到第 天晚上的时候他刷了 题,他的总刷题量为 题,达到了总刷题量的一半。
输入
6
2 2 2 2 2 2
输出
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. #define LL long long
  20. const int maxn = 200000 + 100;
  21. int n, ans;
  22. LL half, sum;
  23. LL num[maxn];
  24. int main() {
  25. #ifdef LOCAL
  26. freopen("test.txt", "r", stdin);
  27. // freopen("out.txt", "w", stdout);
  28. #endif // LOCAL
  29. ios::sync_with_stdio(false);
  30. while(scanf("%d", &n) != EOF) {
  31. half = sum = 0;
  32. for(int i = 1; i <= n; ++i) {
  33. scanf("%I64d", &num[i]);
  34. sum += num[i];
  35. }
  36. for(int i = 1; i <= n; ++i) {
  37. half += num[i];
  38. if(half * 2 >= sum) {
  39. ans = i;
  40. break;
  41. }
  42. }
  43. printf("%d\n", ans);
  44. }
  45. return 0;
  46. }

B. Students in Railway Carriage

题意

在地铁上有 个座位,某些位置上已经被乘客占据,现在有 个写代码的学生和 个运动员学生,问在满足下面条件下,地铁上最多能坐下多少个学生:

  1. 没有任何一个写代码的学生坐在写代码的学生旁边;
  2. 没有任何一个运动员学生坐在运动员学生旁边。

输入

第一行为三个整数 ,第二行为一个长度为 的字符串,字符串只包含 .*,点表示这个位置为空位,星号表示这个位置有乘客占据。

输出

输出最多能够坐得下的学生的数量。

样例

输入
5 1 1
*...*
输出
2
提示
可以让所有学生都有位置坐,如:*.AB*
输入
6 2 3
*...*.
输出
4
提示
可以按 *BAB*B 的方式让 个空位被占满。
输入
11 3 10
.*....**.*.
输出
7
提示
可以按 B*ABAB**A*B 的方式让 个空位被占满。
输入
3 2 3
***
输出
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. #define LL long long
  20. const int maxn = 200000 + 100;
  21. int n, a, b;
  22. char str[maxn];
  23. int main() {
  24. #ifdef LOCAL
  25. freopen("test.txt", "r", stdin);
  26. // freopen("out.txt", "w", stdout);
  27. #endif // LOCAL
  28. ios::sync_with_stdio(false);
  29. while(scanf("%d%d%d", &n, &a, &b) != EOF) {
  30. int sum = a + b;
  31. int flag = 0;
  32. scanf("%s", str);
  33. for(int i = 0; str[i]; ++i) {
  34. if(str[i] == '.') {
  35. if(flag == 0) {
  36. if(a > b) {
  37. flag = 1;
  38. --a;
  39. } else if(b > a) {
  40. flag = 2;
  41. --b;
  42. } else if(a == b && b != 0) {
  43. --b;
  44. flag = 2;
  45. }
  46. } else if(flag == 1) {
  47. if(b > 0) {
  48. --b;
  49. flag = 2;
  50. } else {
  51. flag = 0;
  52. }
  53. } else if(flag == 2) {
  54. if(a > 0) {
  55. --a;
  56. flag = 1;
  57. } else {
  58. flag = 0;
  59. }
  60. }
  61. } else {
  62. flag = 0;
  63. }
  64. }
  65. printf("%d\n", sum - a - b);
  66. }
  67. return 0;
  68. }

C. Make a Square

题意

给定一个不包含前导零的正整数 ,要求删除最少的数字位,使得最终剩下的数字不包含前导零且它是一个完全平方数,或者判断这是无法实现的。

输入

输入只包含一个整数

输出

如果无法实现,则输出 ,否则输出需要的最少的次数。

样例

输入
8314
输出
2
提示
可以删除数字位 ,使得剩下的数字变为 ,它等于
输入
625
输出
0
提示
,因此不需要删除任何数位。
输入
333
输出
-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. #define LL long long
  20. const int maxn = 44721 + 11;
  21. int num;
  22. string str;
  23. int Pow[maxn];
  24. int *it;
  25. set<string> st;
  26. queue<string> que;
  27. bool judge(const string &str) {
  28. if(str[0] == '0') {
  29. return false;
  30. }
  31. int tmp = 0;
  32. int len = str.length();
  33. for(int i = 0; i < len; ++i) {
  34. tmp = tmp * 10 + str[i] - '0';
  35. }
  36. it = lower_bound(Pow + 1, Pow + maxn, tmp);
  37. if(it - Pow != maxn && *it == tmp) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. int bfs() {
  43. string s, tmp;
  44. int len = str.length();
  45. que.push(str);
  46. st.insert(str);
  47. while(!que.empty()) {
  48. tmp = que.front();
  49. que.pop();
  50. if(judge(tmp)) {
  51. return len - tmp.length();
  52. }
  53. int l = tmp.length();
  54. for(int i = 0; i < l; ++i) {
  55. s = tmp.substr(0, i) + tmp.substr(i + 1, l);
  56. if(s != "" && st.find(s) == st.end()) {
  57. st.insert(s);
  58. que.push(s);
  59. }
  60. }
  61. }
  62. return -1;
  63. }
  64. int main() {
  65. #ifdef LOCAL
  66. freopen("test.txt", "r", stdin);
  67. // freopen("out.txt", "w", stdout);
  68. #endif // LOCAL
  69. ios::sync_with_stdio(false);
  70. for(int i = 1; i < maxn; ++i) {
  71. Pow[i] = i * i;
  72. }
  73. while(cin >> str) {
  74. st.clear();
  75. while(!que.empty()) {
  76. que.pop();
  77. }
  78. printf("%d\n", bfs());
  79. }
  80. return 0;
  81. }

D. Merge Equals

题意

给定一个长度为 的序列,多次扫描这个序列,每次扫描如果找到出现至少两次的数字中值最小的数字,将最左边的数字合并到右边的那个数字中,并将右边的数字用这个数字的两倍代替,如对于序列 ,操作步骤如下:

对于序列 的操作如下:
对于给定的序列,输出最终的结果。

输入

第一行为一个整数 ,第二行为 个整数

输出

输出最终的序列。

样例

输入
7
3 4 1 2 2 1 1
输出
4
3 8 2 1
输入
5
1 1 3 1 1
输出
2
3 4
输入
5
10 40 20 50 30
输出
5
10 40 20 50 30

题解

以数字大小为主关键字,数字的下标为次关键字,用大顶堆进行维护,如果两次弹出堆顶的元素相同,则把这个两个数字合并并放回到堆中,否则将小的数字放到答案数组中,另一个数字继续与下一个弹出的元素进行比较。

过题代码

  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. #define LL long long
  20. const int maxn = 150000 + 100;
  21. struct Node {
  22. int Index;
  23. LL num;
  24. Node() {}
  25. Node(int I, LL n) {
  26. Index = I;
  27. num = n;
  28. }
  29. };
  30. bool operator<(const Node &a, const Node &b) {
  31. if(a.num == b.num) {
  32. return a.Index > b.Index;
  33. }
  34. return a.num > b.num;
  35. }
  36. int n;
  37. bool vis[maxn];
  38. LL num[maxn];
  39. priority_queue<Node> que;
  40. int main() {
  41. #ifdef LOCAL
  42. freopen("test.txt", "r", stdin);
  43. // freopen("out.txt", "w", stdout);
  44. #endif // LOCAL
  45. ios::sync_with_stdio(false);
  46. while(scanf("%d", &n) != EOF) {
  47. int cnt = n;
  48. for(int i = 1; i <= n; ++i) {
  49. scanf("%I64d", &num[i]);
  50. que.push(Node(i, num[i]));
  51. vis[i] = true;
  52. }
  53. Node last = que.top();
  54. que.pop();
  55. while(!que.empty()) {
  56. Node tmp = que.top();
  57. que.pop();
  58. if(tmp.num != last.num) {
  59. last = tmp;
  60. continue;
  61. }
  62. --cnt;
  63. vis[last.Index] = false;
  64. num[tmp.Index] = last.num * 2;
  65. que.push(Node(tmp.Index, last.num * 2));
  66. last = que.top();
  67. que.pop();
  68. }
  69. bool flag = false;
  70. printf("%d\n", cnt);
  71. for(int i = 1; i <= n; ++i) {
  72. if(vis[i]) {
  73. if(flag) {
  74. printf(" ");
  75. }
  76. flag = true;
  77. printf("%I64d", num[i]);
  78. }
  79. }
  80. printf("\n");
  81. }
  82. return 0;
  83. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注