Codeforces Round #529 (Div. 3) 题解
刷了一套题散散心,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;
}
不是删原数组中的最小值就是删最大值。
时间复杂度:应该是可以 $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;
}
这个最小生成树还挺好玩。摸索了半天才知道啊。。果然洞察力减弱了。
就是两种边,一种读入的边,另一种原来就有的边,每次怎么取呢?
突破口是样例 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;
}
Codeforces Round #529 (Div. 3) 题解的更多相关文章
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
随机推荐
- Appium+python自动化获取toast消息(windows版)的方法
原来用的Appium1.5.3GUI版本,那为什么升级呢? 为了兼容最新版本的iOS10和Android7 Xcode8升级后,将不支持使用UIAutomation,而是改为使用XCUITest了,并 ...
- 解题:九省联考2018 IIIDX
题面 我当时在考场上划水的时候好像乱搞搞了20pts,然后发现一堆同届的都写了55pts的贪心=.=??? 那就先说那55pts的贪心吧,这个现在看起来还是非常显然的,就是按题意来每一块是分属一个点的 ...
- (转) Eclipse通过HibernateTools实现逆向生成Hibernate实体类
背景:工作中使用Hibernate进行持久化的开发工作,所以有必要详细了解这方面的知识. ps:这里有个问题就是刷新表的时候速度太慢了.还不如自己手动去创建.如果表太多倒是可以采取批量生成的策略. 在 ...
- Linux系统上查找已安装软件的路径
在Linux系统上查找已安装软件路径的命令,以查找pcre的安装路径为例: [root@localhost doc]# rpm -ql pcre /lib64/libpcre.so. /lib64/l ...
- Apache 的 ab 压测工具快速使用
ab 是一个 httpd 自带的很好用的压力测试工具,它是 apache bench 命令的缩写.ab 命令会创建多个并发访问线程,模拟多个访问者同时对某一 URL 地址进行访问.可以用来测试 apa ...
- Java基础-hashMap原理剖析
Java基础-hashMap原理剖析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是哈希(Hash) 答:Hash就是散列,即把对象打散.举个例子,有100000条数 ...
- linux常用端口查询
0 | 无效端口,通常用于分析操作系统1 | 传输控制协议端口服务多路开关选择器2 | 管理实用程序3 | 压缩进程5 | 远程作业登录7 | 回显9 | 丢弃11 | 在线用户13 | 时间17 | ...
- Spark记录-Scala异常处理与文件I/O
Scala的异常处理类似许多其他语言(如Java).它不是以正常方式返回值,方法可以通过抛出异常来终止. 但是,Scala实际上并没有检查异常. 当您想要处理异常时,要像Java一样使用try {.. ...
- bzoj千题计划203:bzoj3994: [SDOI2015]约数个数和
http://www.lydsy.com/JudgeOnline/problem.php?id=3994 设d(x)为x的约数个数,给定N.M,求 用到的一个结论: 证明: 枚举n的约数i,枚举m的约 ...
- scala 资料集结
Scala入门到精通 http://lib.csdn.net/base/scala/structure http://hongjiang.info/scala/ http://blog.csdn.ne ...