# 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,和 ...
随机推荐
- 总结获取原生JS(javascript)基本操作
var a = document.getElementByIdx_x_x("dom"); jsCopy(a);//调用清理空格的函数 var b = a.childNodes;// ...
- Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking---随笔
Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking DCF跟踪算法因边界效应,鲁棒性较差.SRD ...
- 【转载】图解Java常用数据结构(一)
图解Java常用数据结构(一) 作者:大道方圆 原文:https://www.cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识, 系统化看了下Jav ...
- vs2008 c#项目调试dll源码,问题:“若要调试此模块,请将其项目生成配置更改为“调试”模式” 的解决方案
情况: 1:有程序 Trans.exe 的vs2008 c#源码:Trans.exe项目里引用了 Water.dll: 2:有Water.dll的项目源码: 3:想在Trans.exe里调试Water ...
- Keil sct分散加载文件
官方说明:http://www.keil.com/support/man/docs/armlink/armlink_pge1401393372646.htm
- Spark GraphX 2
顶点:VertexRDD 边:EdgeRDD.Edge.EdgeDirection Triplet:EdgeTriplet 存储:PartitionStrategy 通常的存储方式有两种: ...
- 软工1816 · Alpha冲刺(3/10)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 完成了对laravel框架的一整套机制的了解,对后端的处理流程有全面的认识对整 ...
- C#中委托的发展与匿名函数
匿名函数(C# 编程指南) 匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数. 共有两种匿名函数 ...
- eg_2
2. 编写一个程序,输出在一个字符串中,指定的字符串出现的次数 第一种方法: public class Test { public static void main(String[] args) { ...
- 用glob()函数返回目录下的子文件以及子目录
glob() 函数返回匹配指定模式的文件名或目录 相对于readdir()和opendir()来说,使用glob()函数会方便很多 代码1: <?php function getfilename ...