刷了一套题散散心,Div 3,全部是 1 A,感觉比以前慢了好多好多啊。

这几天也整理了一下自己要做的事情,工作上要努力... ... 晚上还是要认认真真背英语的。

找了几个同学问了一下,有点想自己做点 project 了,再学学机器学习,深度学习之类的,弄点小 AI project 玩玩吧... ...没事看点各种科技新闻开开眼界。

【题目链接】

A - Repeating Cipher

挺简单的,只要知道哪几个位置要输出就可以了。

时间复杂度:$O(N)$

#include <bits/stdc++.h>
using namespace std;const int maxn = 1e5 + 10;
char s[maxn], len;int main() {scanf("%d", &len);scanf("%s", s);int p = 0;for(int i = 0; i < len; i = i + p) {printf("%c", s[i]);p++;}printf("\n");return 0;
}

  

B - Array Stabilization

不是删原数组中的最小值就是删最大值。

时间复杂度:应该是可以 $O(N)$ 实现的吧,但是排个序 $O(N*logN)$ 写起来多简单。

#include <bits/stdc++.h>
using namespace std;const int maxn = 1e5 + 10;
int a[maxn], n;int main() {scanf("%d", &n);for(int i = 1; i <= n; i ++) {scanf("%d", &a[i]);}sort(a + 1, a + 1 + n);printf("%d\n", min(a[n - 1] - a[1], a[n] - a[2]));return 0;
}

  

C - Powers Of Two

先将 $N$ 转换成二进制,如果不到 $k$ 个,那么可以找一个数字出来,除以 $2$,拆成两个,这样就多了一个,按这样慢慢操作就好了。

时间复杂度:在实现的时候我把所有数字扔进了优先队列,每次拆最大的那个,事实上每次拆一个可拆的就可以了。$O(K*logK)$。

#include <bits/stdc++.h>
using namespace std;int n, k;priority_queue<int> Q;int cnt_Q;int main() {scanf("%d%d", &n, &k);for(int i = 29; i >= 0; i --) {if(n >= (1 << i)) {Q.push(i);cnt_Q ++;n -= (1 << i);}}if(cnt_Q > k) {printf("NO\n");return 0;}while(cnt_Q < k) {int tp = Q.top();if(tp == 0) {printf("NO\n");return 0;}Q.pop();Q.push(tp - 1);Q.push(tp - 1);cnt_Q ++;}printf("YES\n");while(!Q.empty()) {int tp = Q.top();printf("%d ", 1 << tp);Q.pop();}return 0;
}

  

D - Circular Dance

这题比较逗,数据保证一定有解。emmmm... 那是不是很大概率填完就是可行解呢?我也不太会证明,反正这样写了一下就 AC 了,写之前就挺有把握。

复杂度可能是线性的?

#include <bits/stdc++.h>
using namespace std;const int maxn = 2e5 + 10;int n;
int a[maxn];int b[maxn][5];int flag;void dfs(int x) {int num1 = b[a[x]][1];int num2 = b[a[x]][2];if(b[num1][1] == num2 || b[num1][2] == num2) {a[x + 1] = num1;a[x + 2] = num2;if(x == n - 2) {flag = 1;return;}dfs(x + 1);if(flag) return;}if(b[num2][1] == num1 || b[num2][2] == num1) {a[x + 1] = num2;a[x + 2] = num1;if(x == n - 2) {flag = 1;return;}dfs(x + 1);if(flag) return;}
}int main() {scanf("%d", &n);for(int i = 1; i <= n; i ++) {scanf("%d%d", &b[i][1], &b[i][2]);}flag = 0;a[1] = 1;dfs(1);for(int i = 1; i <= n; i ++) {printf("%d ", a[i]);}printf("\n");return 0;
}

E - Almost Regular Bracket Sequence

这个题意是,问你有几个位置,只改变这个位置,能让它变成合法括号匹配串。

括号匹配常见套路。左括号变成 1,右括号变成 -1,算前缀和 $S_i$。

一个合法的括号匹配串的充要条件是:[1] 对于任何 $i$, $S_i$ 都非负。[2] $S_n = 0$。

balabalabala.....

然后你大概就会做了。

#include <bits/stdc++.h>
using namespace std;const int maxn = 1e6 + 10;int n;
int a[maxn], b[maxn], c[maxn];
char s[maxn];int main() {scanf("%d", &n);scanf("%s", s);for(int i = 1; i <= n; i ++) {a[i] = (s[i - 1] == '(') ? 1 : -1;}for(int i = 1; i <= n; i ++) {a[i] += a[i - 1];}c[1] = a[1];for(int i = 2; i <= n; i ++) {c[i] = min(a[i], c[i - 1]);}b[n] = a[n];for(int i = n - 1; i >= 1; i --) {b[i] = min(a[i], b[i + 1]);}for(int i = 1; i <= n; i ++) {// printf("[i: %d]  a: %d, b: %d, c: %d\n", i, a[i], b[i], c[i]);}int ans = 0;for(int i = 1; i <= n; i ++) {if(s[i - 1] == '(') {if(c[i - 1] >= 0 && b[i] - 2 >= 0 && a[n] - 2 == 0) ans ++;} else {if(c[i - 1] >= 0 && b[i] + 2 >= 0 && a[n] + 2 == 0) ans ++;}}printf("%d\n", ans);return 0;
}

  

F - Make It Connected

这个最小生成树还挺好玩。摸索了半天才知道啊。。果然洞察力减弱了。

就是两种边,一种读入的边,另一种原来就有的边,每次怎么取呢?

突破口是样例 2,先看看 $m$ 是 0 的时候要怎么弄?也就是全是原来的边的时候答案是怎么来的。

画画图就能知道是哪些边了... ... 我就不写了。

$m$ 不是 0 的时候,那岂不是就是把这些边和读入的边合起来做个 MST 吗...

The world is so funny, but I am so naive.

#include <bits/stdc++.h>
using namespace std;const int maxn = 4e5 + 10;
struct Edge {int x, y;long long w;
}e[maxn];
int cnt_e;int n, m;
long long a[maxn];int f[maxn];bool cmp(const Edge& a, const Edge& b) {return a.w < b.w;
}int Find(int x) {if(x != f[x]) f[x] = Find(f[x]);return f[x];
}int main() {scanf("%d%d", &n, &m);for(int i = 1; i <= n; i ++) {scanf("%lld", &a[i]);f[i] = i;}int index = 1;for(int i = 2; i <= n; i ++) {if(a[i] < a[index]) {index = i;}}for(int i = 1; i <= n; i ++) {if(i == index) continue;e[cnt_e].x = i;e[cnt_e].y = index;e[cnt_e].w = a[i] + a[index];cnt_e ++;}while(m--) {scanf("%d%d%lld", &e[cnt_e].x, &e[cnt_e].y, &e[cnt_e].w);cnt_e ++;}sort(e, e + cnt_e, cmp);long long ans = 0;for(int i = 0; i < cnt_e; i ++) {int fx = Find(e[i].x);int fy = Find(e[i].y);if(fx == fy) continue;ans = ans + e[i].w;f[fx] = fy;}printf("%lld\n", ans);return 0;
}

  

转载于:https://www.cnblogs.com/zufezzt/p/10587249.html

Codeforces Round #529 (Div. 3) 题解相关推荐

  1. Codeforces Round #514 (Div. 2)题解

    Codeforces Round #514 (Div. 2)题解 A 喵,直接模拟. B 枚举所有盖章时的,合法的,左上角的位置.能盖的话就盖一下.最后check一下图案是否相等即可 C 一轮一轮的扔 ...

  2. [CF]Codeforces Round #529 (Div. 3)

    [CF]Codeforces Round #529 (Div. 3) C. Powers Of Two Description A positive integer xx is called a po ...

  3. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  4. 【算法题解】Codeforces Round #817 (Div. 4)题解

    文章目录 Codeforces Round #817 (Div. 4)题解 A. Spell Check B. Colourblindness C. Word Game D. Line E. Coun ...

  5. Codeforces Round #747 (Div. 2)题解

    Codeforces Round #747 (Div. 2)题解 (本博客将持续更新以后每场CF div2的题解,喜欢ACM.OI的小伙伴记得点个关注哟) 昨天夜晚刷网络流刷入迷了,渐渐就忘记了我还要 ...

  6. Codeforces Round #789 (Div. 2)题解

    Codeforces Round #789 (Div. 2)题解 A. Tokitsukaze and All Zero Sequence 原题链接 算法标签 贪心 排序 思路 情况一:数组存在零 → ...

  7. Codeforces Round #748 (Div. 3) 题解 完整A~G

    Codeforces Round #748 (Div. 3) 题解 A. Elections 题意 已知竞选中三个候选人的当前得票数 a , b , c a,b,c a,b,c,现在可以增加任何一个人 ...

  8. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

  9. Codeforces Round #734 (Div. 3) 题解

    Hello大家好,今天给大家带来的是 Codeforces Round #734 (Div. 3) 的全题目讲解. 本文链接:https://www.lanqiao.cn/questions/2040 ...

最新文章

  1. FM-2018-大熊猫的肠道菌群可能并没有特化出发酵纤维素的能力
  2. 面向对象的基本设计原则【转】
  3. 从JAVA转学习Go——Go在eclipse的环境搭建
  4. unity3d做会减少的血条_Unity3d中NGUI加强版血条(Healthbar)的制作
  5. J - 数据结构实验:哈希表
  6. 用区块链改变人工智能:去中心化带来数据新范式
  7. 【安全牛学习笔记】思路、身份认证方法、密码破解方法、字典
  8. Codeforces 821C - Okabe and Boxes
  9. PowerDesigner从数据库导入
  10. 利用Continuous Testing实现Eclipse环境自己主动单元測试
  11. 参与就有1000块,30W奖池你占一半 | 帆软开发者大赛招募
  12. capture RGBD data with opencv4.1.1 from XTION2 on windows 10
  13. 戴尔笔记本怎么重装系统win11,win11系统安装方法
  14. linux gulp安装教程,Linux环境NodeJS安装及Gulp安装
  15. KeyPass密码管理软件使用说明
  16. 云MAS中CMPP3.0协议封装与移动短信状态报告状态码说明
  17. imperva数据库脱敏-server2008
  18. c++八大排序算法详解
  19. 瀚高数据库不定时停库
  20. 编写用户故事模板_编写踢屁股用户故事

热门文章

  1. linux下内存的统计和内存泄露问题的定位-转
  2. 公务员计算机基础知识笔记,公务员计算机基础知识【精选】.doc
  3. java反编译工具jad和jd-gui使用
  4. [Java] 蓝桥杯BASIC-11 基础练习 十六进制转十进制
  5. PAT 乙级 1046. 划拳(15) Java版
  6. 【note】Swift之闭包表达式的几种简化缩写形式
  7. Python出现quot; SyntaxError: Non-ASCII character '\xe6' 或'\xd6' in filequot;错误解决方法
  8. JDBC连接数据库教程,以postgreSQL为例
  9. Eclipse console 编码设置
  10. 学习Kubernetes 和容器技术体系的最佳方法