当前位置:   article > 正文

AtCoder Beginner Contest 363

AtCoder Beginner Contest 363

A - Piling Up

题意

不同的分数段有不同的^数量,Takahashi想要使得他的^数量增加,问他所需要的最少分数增幅。

思路

我们只需要找到下一阶段的下限。

a / 100 是本阶段

+1 变成下一阶段,再 * 100变成下限,再与原来的相减即可。

代码

  1. inline void solve() {
  2. int a; cin >> a;
  3. cout << (a / 100 + 1) * 100 - a << endl;
  4. return;
  5. }

B - Japanese Cursed Doll

题意

有n个人,每个人的头发长度每天都会增加1,问至少有p个人的头发长度大于等于T的最小天数。

思路

我们只需要找到第k头发长的人。

如果他已经比T大了,就说明一开始就满足,输出0

否则找到他与T的差值,就是最小天数

代码

  1. inline void solve() {
  2. int n, t, p; cin >> n >> t >> p;
  3. vector<int> a(n + 1);
  4. for (int i = 1; i <= n; i ++ ) cin >> a[i];
  5. nth_element(a.begin() + 1, a.begin() + n - p + 1, a.end());
  6. cout << max(t - a[n - p + 1], 0) << endl;
  7. return;
  8. }

C - Avoid K Palindrome 2 

题意

给你一个字符串,你可以任意改变其中的字符位置,问不包含长度为k的回文子串的字符串数量有多少个。

思路

数据范围很小,最差阶乘的复杂度,直接暴力模拟即可。

代码

  1. inline void solve() {
  2. int n, k; cin >> n >> k;
  3. string s; cin >> s;
  4. vector<int> a(n);
  5. for (int i = 0; i < n; i ++ ) a[i] = s[i] - 'a';
  6. sort(a.begin(), a.end());
  7. function<bool()> check = [&]() {
  8. for (int i = 0; i <= n - k; i ++ ) {
  9. int c = i + k - 1;
  10. bool flag = true;
  11. for (int j = i; j < c; j ++ , c -- ) {
  12. if (a[j] != a[c]) {
  13. flag = false;
  14. }
  15. }
  16. if (flag) return true;
  17. }
  18. return false;
  19. };
  20. int ans = 0;
  21. do {
  22. if (!check()) ans += 1;
  23. } while (next_permutation(a.begin(), a.end()));
  24. cout << ans << endl;
  25. return;
  26. }

 D - Palindromic Number

题意

求第N小的回文数

思路

首先我们可以观察对于i位所含的回文数的数量,这可以帮助我们找到第N个在哪

1位  10个

2位    9个

3位  90个

4位  90个

5位  900个

其实也很好推,对于1和2位,自己手玩就可以知道了。

然后对于位数为奇数的,相当于在之前位数为偶数的中间插入0~9

xx0xx

xx1xx

...

所以奇数位所含的回文数数量是上一位的10倍

而偶数位与上一位所含相同

因为

111 --必须是->   1111

121 --必须是->  1221

那我们只要一开始减1,然后按此操作 -9 -9 - 90 -90 -900 ...就可以知道它有几位了。

然后,回文的进位和正常的进位的区别不就在于它是向两边进位吗,所以我们只需观察中间。

比如

191 --> 202

1991 -->  2002

又我们观察到的数据是1e18,输入可以是ll,但是输出要是字符串。

所以我们可以用字符串维护一个前缀。

然后奇数位的话会影响到到下一位的数量

4位有90个回文数,5位有900个

偶数位的话会影响到前缀

代码

  1. inline void solve() {
  2. ll x; cin >> x;
  3. x -= 1;
  4. string pre;
  5. ll nd = -1;
  6. for (int d = 1; ; d ++ ) {
  7. if (d & 1) {
  8. if (nd == -1) nd = 9;
  9. else nd *= 10;
  10. if (x <= nd) {
  11. if (!pre.size()) return cout << x << endl, void();
  12. x -= 1;
  13. ll num = stoll(pre) + x / 10;
  14. string pre = to_string(num);
  15. string s = pre + to_string(x % 10);
  16. reverse(pre.begin(), pre.end());
  17. s += pre;
  18. cout << s << endl;
  19. return;
  20. }
  21. x -= nd;
  22. }else {
  23. if (x <= nd) {
  24. if (!pre.size()) return cout << x << x << endl, void();
  25. x -= 1;
  26. ll num = stoll(pre) + x / 10;
  27. string pre = to_string(num);
  28. string s = pre + to_string(x % 10 * 11);
  29. reverse(pre.begin(), pre.end());
  30. s += pre;
  31. cout << s << endl;
  32. return;
  33. }
  34. x -= nd;
  35. if (!pre.size()) pre = "1";
  36. else pre += "0";
  37. }
  38. }
  39. return;
  40. }

 E - Sinking Land

题意

当地段高度小于等于海平面的时候就可以称之为沉没,海平面每年升高1,问今后的Y年中各有多少个地段没有沉没?

思路

模拟题,我用了优先队列优化的bfs,我们只需要把跟海接触的放到queue里面,然后一直判断即可。

代码

  1. int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
  2. struct node {
  3. int x, y, v;
  4. bool operator > (const node& a) const {
  5. return v > a.v;
  6. }
  7. };
  8. inline void solve() {
  9. int n, m, k; cin >> n >> m >> k;
  10. vector<vector<int>> a(n + 1, vector<int>(m + 1)), vis(n + 1, vector<int>(m + 1));
  11. priority_queue<node, vector<node>, greater<node>> q;
  12. for (int i = 1; i <= n; i ++ ) {
  13. for (int j = 1; j <= m; j ++ ) {
  14. cin >> a[i][j];
  15. if (i == 1 || i == n || j == 1 || j == m) q.push({i, j, a[i][j]}), vis[i][j] = 1;
  16. }
  17. }
  18. int ans = n * m;
  19. for (int i = 1; i <= k; i ++ ) {
  20. while (q.size() && q.top().v <= i) {
  21. node T = q.top(); q.pop();
  22. int x = T.x, y = T.y, v = T.v;
  23. ans -= 1;
  24. for (int i = 0; i < 4; i ++ ) {
  25. int x1 = x + dx[i], y1 = y + dy[i];
  26. if (x1 < 1 || x1 > n || y1 < 1 || y1 > m || vis[x1][y1]) continue;
  27. q.push({x1, y1, a[x1][y1]});
  28. vis[x1][y1] = 1;
  29. }
  30. }
  31. cout << ans << endl;
  32. }
  33. return;
  34. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/864242
推荐阅读
相关标签
  

闽ICP备14008679号