# Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解
前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来
A. Repeating Cipher
题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么
题解:1、2、3、4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可
代码:
#include<bits/stdc++.h>
using namespace std;
int num[100000];
void init(){
int ans=0;
for(int i=1;i<=1000;i++){
ans+=i;
num[i]=ans;
}
return;
}
char str[10000];
int main(){
int n;
init();
scanf("%d %s",&n,str+1);
int tmp=1;
while(num[tmp]!=n){
tmp++;
}
for(int i=1;i<=tmp;i++){
cout<<str[num[i]];
}
cout<<endl;
}
B. Array Stabilization
题意:给你一串数字,要你删除一个数最小化这串数字中最大值-最小值的差
题解:用multiset存一下,然后讨论删去最大的数更好还是删去最小的数更好
代码:
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int maxn = 3e5 + 5;
const LL INF = 1e18 + 7;
const ull mod = 9223372034707292160;
LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
LL powmod(LL a, LL b, LL MOD) {LL ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
double dpow(double a, LL b) {double ans = 1.0; while (b) {if (b % 2)ans = ans * a; a = a * a; b /= 2;} return ans;}
multiset<int> s;
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n;
cin >> n;
int x;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
s.insert(x);
}
multiset<int>::iterator it;
it = s.begin();
int minn = *it;
it = s.end();
it--;
int maxx = *it;
if (s.count(minn) != 1 && s.count(maxx) != 1) {
cout << maxx - minn << endl;
} else if (s.count(minn) == 1 && s.count(maxx) == 1) {
it = s.begin();
it++;
int tmp1 = *it;
it = s.end();
it--;
it--;
int tmp2 = *it;
int ans1 = maxx - tmp1;
int ans2 = tmp2 - minn;
cout << min(ans1, ans2) << endl;
} else {
if (s.count(minn) == 1) {
it = s.begin();
it++;
cout << maxx - *it << endl;
} else {
it = s.end();
it--;
it--;
cout << *it - minn << endl;
}
}
}
C. Powers Of Two
题意:给你一个数n,要求你用k个2的幂次数去拼出这个数,如果不能输出-1
题解:先将n转换为相对应的二进制数 ,如果n的二进制数中的1的个数大于k,显然是没有解的,如果n的二进制数中的1的个数小于1,那么就把每一个大于2的数分解(x->x/2+x/2),凑出k个1即可,最后输出 一下就行
代码
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int maxn = 3e5 + 5;
const LL INF = 1e18 + 7;
const ull mod = 9223372034707292160;
LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
LL powmod(LL a, LL b, LL MOD) {LL ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
double dpow(double a, LL b) {double ans = 1.0; while (b) {if (b % 2)ans = ans * a; a = a * a; b /= 2;} return ans;}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
LL n, k;
cin >> n >> k;
if (k > n) {
cout << "NO" << endl;
} else {
multiset<int> ans;
multiset<int>::iterator it;
for (int i = 0; i < 30; i++)
if ((n >> i) & 1)
ans.insert(i);
if (ans.size() > k)
{
cout << "NO";
return 0;
}
cout << "YES\n";
while ((int)ans.size() < k)
{
it = ans.end();
it--;
int x = (*it);
ans.erase(ans.lower_bound(x));
ans.insert(x - 1);
ans.insert(x - 1);
}
for (it = ans.begin(); it != ans.end(); it++)
cout << (1 << *it) << " ";
return 0;
}
}
D. Circular Dance
题意:n个人围成一圈,每个人报出接下来两个人的序号,但是不保证按照顺序来,求解这一圈人的编号顺序,题目有spj
题解:将每个人报的编号想成两个点,然后就行成了一个图的关系,那么现在我们就只需要判定这个图的连通性即可
代码:
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int maxn = 3e5 + 5;
const LL INF = 1e18 + 7;
const ull mod = 9223372034707292160;
LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
LL powmod(LL a, LL b, LL MOD) {LL ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
double dpow(double a, LL b) {double ans = 1.0; while (b) {if (b % 2)ans = ans * a; a = a * a; b /= 2;} return ans;}
vector<int> ans;
vector<int> mp[maxn];
bool check(int a, int b) {
for (int i = 0; i < mp[a].size(); i++) {
if (mp[a][i] == b) {
return 1;
}
}
return 0;
}
int get_next(int x){
int v1=mp[x][0];
int v2=mp[x][1];
if(check(v1,v2)){
return v1;
}
return v2;
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n;
scanf("%d", &n);
int u, v;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &u, &v);
mp[i].push_back(u);
mp[i].push_back(v);
}
if (n == 3) {
cout << "1 2 3" << endl;
return 0;
}
ans.push_back(1);
while (ans.size() < n) {
int val = ans.back();
int nxt = get_next(val);
ans.push_back(nxt);
}
for (int i = 0; i < ans.size(); i++) {
if (i)
cout << " ";
cout << ans[i];
}
cout << endl;
}
E. Almost Regular Bracket Sequence
题意:给你一个括号序列,你需要翻转其中一个括号使得括号序列合法,求应该翻哪个,有spj
题解:我们可以用前缀和来很好的解决括号匹配问题,首先我们规定‘(’是1,‘)’是-1求出这个序列的前缀和,然后用一个数组来记录从后往前的前缀和的最小值,最后从前往后扫一遍,判断翻转这个位置是否能够使得序列合法即可
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
string str;
int n;
bool check(string str){
int len=str.length();
int tmp=len/2;
for(int i=0;i<tmp;i++){
if(str[i]!=str[len-i]) return 0;
}
return 1;
}
int sum[maxn];
int minn[maxn];
int main(){
cin>>n>>str;
sum[0]=0;
for(int i=0;i<n;i++){
if(str[i]=='(') sum[i+1]=sum[i]+1;
else if(str[i]==')') sum[i+1]=sum[i]-1;
}
minn[n]=sum[n];
for(int i=n-1;i>=0;i--){
minn[i]=min(minn[i+1],sum[i]);
}
int ans=0;
for(int i=0;i<n;i++){
if(sum[i]<0) break;
int tmp=sum[i];
if(str[i]==')'){
tmp++;
}else if(str[i]=='('){
tmp--;
}
if(tmp<0) continue;
if(sum[n]-sum[i+1]+tmp!=0) continue;
if(minn[i+1]-sum[i+1]+tmp<0) continue;
ans++;
}
cout<<ans<<endl;
}
F. Make It Connected
题意:给你一个无向图,有n个点,每个点有一个权值,从a点走到b点的花费是a、b的权值和,有m条边可以连接,如果连接u和v则花费w的权值,当然也可以选择不连,求使得这个图联通的最小花费
题解:我们找到一个起点,要想使得这个生成这个图的花费最小,那么起点一定是权值最小的那个,连边时将这个起点和所有的点连接起来,然后最后跑一个最小生成树即可
代码
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int maxn = 3e5 + 5;
const LL INF = 1e18 + 7;
const ull mod = 9223372034707292160;
LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
LL powmod(LL a, LL b, LL MOD) {LL ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
double dpow(double a, LL b) {double ans = 1.0; while (b) {if (b % 2)ans = ans * a; a = a * a; b /= 2;} return ans;}
int n, m;
LL a[maxn];
struct EDGE {
int u, v;
LL w;
bool operator < (const EDGE&a) {
return w < a.w;
}
}edge[maxn<<2];
int f[maxn];
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
cin>>n>>m;
int minn=0;
a[0]=INF;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
if(a[i]<a[minn]) minn=i;
}
for(int i=1;i<=n;i++){
edge[i]=(EDGE){i,minn,a[i]+a[minn]};
f[i]=i;
}
for(int i=n+1;i<=n+m;i++){
scanf("%d%d%lld",&edge[i].u,&edge[i].v,&edge[i].w);
}
sort(edge+1,edge+1+n+m);
LL ans=0;
for(int i=1;i<=n+m;i++){
int u=find(edge[i].u);
int v=find(edge[i].v);
if(u!=v){
f[u]=v;
ans+=edge[i].w;
}
}
cout<<ans<<endl;
}
# Codeforces Round #529(Div.3)个人题解的更多相关文章
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- Codeforces Round #545 (Div. 1) 简要题解
这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...
- Codeforces Round #624 (Div. 3)(题解)
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...
随机推荐
- 使用getid3获取音频文件信息
今天有个需求,在上传音频文件时候自动获取音频的秒数,和大家分享一下. 首先把getid3的包下载下来 链接:https://pan.baidu.com/s/1Qmdj-I4boz9Sm9GFsON0D ...
- TW实习日记:第13天
昨天困扰的问题终于解决了.因为是百度地图api提供的函数,所以这个解决办法并不适用于所有异步请求,仅仅针对百度地图api的调用接口函数和回调函数.有两种解决方法可以解决百度地图api中常出现的请求回调 ...
- ORACLE高级部分内容
1.pl/sql基本语句 DECLARE BEGIN END; / 循环语句 DECLARE I NUMBER(2):=1; BEGIN WHILE I<100 LOOP I:=I+1; EN ...
- 孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解
孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解 (今天由于文中所阐述的原因没有进行屏幕录屏,见谅) 为了能够使用selenium模块进行真正的操作,今天主要大范围搜索资料进行 ...
- ionic 日期插件学习
<ion-header> <ion-navbar> <ion-title> DateTime </ion-title> </ion-navbar& ...
- CVPR2018 关于视频目标跟踪(Object Tracking)的论文简要分析与总结
本文转自:https://blog.csdn.net/weixin_40645129/article/details/81173088 CVPR2018已公布关于视频目标跟踪的论文简要分析与总结 一, ...
- 4-2:实现cp命令
#include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h& ...
- psp1111
1 本周psp 2.本周进度条 3.本周累积进度图 代码累积折线图 博文字数累积折线图 4.本周PSP饼状图
- M2功能规格说明书
1.目的: 这篇随笔是简述我们团队所做的工程所能实现的功能及方便用户的使用. 2.假定和约束: 我们先限定为本地连接数据库进行各种操作的实现.用户电脑中需要有FLASH工具及快播插件.其他只需要了解基 ...
- Alpha项目冲刺_博客链接合集
组员 学号 林泽宇(队长) 211606317 李涵 211606365 尹海川 211606388 郏敏杰 211606307 何永康 211606362 陈炳旭 211606353 苏宇翔 211 ...