Codeforces Round #529(Div.3)个人题解

前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来


A. Repeating Cipher

传送门

题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么

题解:1、2、3、4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int num[100000];
  4. void init(){
  5. int ans=0;
  6. for(int i=1;i<=1000;i++){
  7. ans+=i;
  8. num[i]=ans;
  9. }
  10. return;
  11. }
  12. char str[10000];
  13. int main(){
  14. int n;
  15. init();
  16. scanf("%d %s",&n,str+1);
  17. int tmp=1;
  18. while(num[tmp]!=n){
  19. tmp++;
  20. }
  21. for(int i=1;i<=tmp;i++){
  22. cout<<str[num[i]];
  23. }
  24. cout<<endl;
  25. }

B. Array Stabilization

传送门

题意:给你一串数字,要你删除一个数最小化这串数字中最大值-最小值的差

题解:用multiset存一下,然后讨论删去最大的数更好还是删去最小的数更好

代码:

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <stack>
  6. #include <queue>
  7. #include <cstdio>
  8. #include <cctype>
  9. #include <bitset>
  10. #include <string>
  11. #include <vector>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <functional>
  16. #define PI acos(-1)
  17. #define eps 1e-8
  18. #define fuck(x) cout<<#x<<" = "<<x<<endl;
  19. #define lson l,mid,rt<<1
  20. #define rson mid+1,r,rt<<1|1
  21. #define FIN freopen("input.txt","r",stdin);
  22. #define FOUT freopen("output.txt","w+",stdout);
  23. //#pragma comment(linker, "/STACK:102400000,102400000")
  24. using namespace std;
  25. typedef long long LL;
  26. typedef unsigned long long ull;
  27. typedef pair<int, int> PII;
  28. const int maxn = 3e5 + 5;
  29. const LL INF = 1e18 + 7;
  30. const ull mod = 9223372034707292160;
  31. LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
  32. LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
  33. 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;}
  34. 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;}
  35. multiset<int> s;
  36. int main() {
  37. #ifndef ONLINE_JUDGE
  38. FIN
  39. #endif
  40. int n;
  41. cin >> n;
  42. int x;
  43. for (int i = 0; i < n; i++) {
  44. scanf("%d", &x);
  45. s.insert(x);
  46. }
  47. multiset<int>::iterator it;
  48. it = s.begin();
  49. int minn = *it;
  50. it = s.end();
  51. it--;
  52. int maxx = *it;
  53. if (s.count(minn) != 1 && s.count(maxx) != 1) {
  54. cout << maxx - minn << endl;
  55. } else if (s.count(minn) == 1 && s.count(maxx) == 1) {
  56. it = s.begin();
  57. it++;
  58. int tmp1 = *it;
  59. it = s.end();
  60. it--;
  61. it--;
  62. int tmp2 = *it;
  63. int ans1 = maxx - tmp1;
  64. int ans2 = tmp2 - minn;
  65. cout << min(ans1, ans2) << endl;
  66. } else {
  67. if (s.count(minn) == 1) {
  68. it = s.begin();
  69. it++;
  70. cout << maxx - *it << endl;
  71. } else {
  72. it = s.end();
  73. it--;
  74. it--;
  75. cout << *it - minn << endl;
  76. }
  77. }
  78. }

C. Powers Of Two

传送门

题意:给你一个数n,要求你用k个2的幂次数去拼出这个数,如果不能输出-1

题解:先将n转换为相对应的二进制数 ,如果n的二进制数中的1的个数大于k,显然是没有解的,如果n的二进制数中的1的个数小于1,那么就把每一个大于2的数分解(x->x/2+x/2),凑出k个1即可,最后输出 一下就行

代码

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <stack>
  6. #include <queue>
  7. #include <cstdio>
  8. #include <cctype>
  9. #include <bitset>
  10. #include <string>
  11. #include <vector>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <functional>
  16. #define PI acos(-1)
  17. #define eps 1e-8
  18. #define fuck(x) cout<<#x<<" = "<<x<<endl;
  19. #define lson l,mid,rt<<1
  20. #define rson mid+1,r,rt<<1|1
  21. #define FIN freopen("input.txt","r",stdin);
  22. #define FOUT freopen("output.txt","w+",stdout);
  23. //#pragma comment(linker, "/STACK:102400000,102400000")
  24. using namespace std;
  25. typedef long long LL;
  26. typedef unsigned long long ull;
  27. typedef pair<int, int> PII;
  28. const int maxn = 3e5 + 5;
  29. const LL INF = 1e18 + 7;
  30. const ull mod = 9223372034707292160;
  31. LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
  32. LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
  33. 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;}
  34. 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;}
  35. int main() {
  36. #ifndef ONLINE_JUDGE
  37. FIN
  38. #endif
  39. LL n, k;
  40. cin >> n >> k;
  41. if (k > n) {
  42. cout << "NO" << endl;
  43. } else {
  44. multiset<int> ans;
  45. multiset<int>::iterator it;
  46. for (int i = 0; i < 30; i++)
  47. if ((n >> i) & 1)
  48. ans.insert(i);
  49. if (ans.size() > k)
  50. {
  51. cout << "NO";
  52. return 0;
  53. }
  54. cout << "YES\n";
  55. while ((int)ans.size() < k)
  56. {
  57. it = ans.end();
  58. it--;
  59. int x = (*it);
  60. ans.erase(ans.lower_bound(x));
  61. ans.insert(x - 1);
  62. ans.insert(x - 1);
  63. }
  64. for (it = ans.begin(); it != ans.end(); it++)
  65. cout << (1 << *it) << " ";
  66. return 0;
  67. }
  68. }

D. Circular Dance

传送门

题意:n个人围成一圈,每个人报出接下来两个人的序号,但是不保证按照顺序来,求解这一圈人的编号顺序,题目有spj

题解:将每个人报的编号想成两个点,然后就行成了一个图的关系,那么现在我们就只需要判定这个图的连通性即可

代码:

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <stack>
  6. #include <queue>
  7. #include <cstdio>
  8. #include <cctype>
  9. #include <bitset>
  10. #include <string>
  11. #include <vector>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <functional>
  16. #define PI acos(-1)
  17. #define eps 1e-8
  18. #define fuck(x) cout<<#x<<" = "<<x<<endl;
  19. #define lson l,mid,rt<<1
  20. #define rson mid+1,r,rt<<1|1
  21. #define FIN freopen("input.txt","r",stdin);
  22. #define FOUT freopen("output.txt","w+",stdout);
  23. //#pragma comment(linker, "/STACK:102400000,102400000")
  24. using namespace std;
  25. typedef long long LL;
  26. typedef unsigned long long ull;
  27. typedef pair<int, int> PII;
  28. const int maxn = 3e5 + 5;
  29. const LL INF = 1e18 + 7;
  30. const ull mod = 9223372034707292160;
  31. LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
  32. LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
  33. 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;}
  34. 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;}
  35. vector<int> ans;
  36. vector<int> mp[maxn];
  37. bool check(int a, int b) {
  38. for (int i = 0; i < mp[a].size(); i++) {
  39. if (mp[a][i] == b) {
  40. return 1;
  41. }
  42. }
  43. return 0;
  44. }
  45. int get_next(int x){
  46. int v1=mp[x][0];
  47. int v2=mp[x][1];
  48. if(check(v1,v2)){
  49. return v1;
  50. }
  51. return v2;
  52. }
  53. int main() {
  54. #ifndef ONLINE_JUDGE
  55. FIN
  56. #endif
  57. int n;
  58. scanf("%d", &n);
  59. int u, v;
  60. for (int i = 1; i <= n; i++) {
  61. scanf("%d%d", &u, &v);
  62. mp[i].push_back(u);
  63. mp[i].push_back(v);
  64. }
  65. if (n == 3) {
  66. cout << "1 2 3" << endl;
  67. return 0;
  68. }
  69. ans.push_back(1);
  70. while (ans.size() < n) {
  71. int val = ans.back();
  72. int nxt = get_next(val);
  73. ans.push_back(nxt);
  74. }
  75. for (int i = 0; i < ans.size(); i++) {
  76. if (i)
  77. cout << " ";
  78. cout << ans[i];
  79. }
  80. cout << endl;
  81. }

E. Almost Regular Bracket Sequence

传送门

题意:给你一个括号序列,你需要翻转其中一个括号使得括号序列合法,求应该翻哪个,有spj

题解:我们可以用前缀和来很好的解决括号匹配问题,首先我们规定‘(’是1,‘)’是-1求出这个序列的前缀和,然后用一个数组来记录从后往前的前缀和的最小值,最后从前往后扫一遍,判断翻转这个位置是否能够使得序列合法即可

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e6+5;
  4. string str;
  5. int n;
  6. bool check(string str){
  7. int len=str.length();
  8. int tmp=len/2;
  9. for(int i=0;i<tmp;i++){
  10. if(str[i]!=str[len-i]) return 0;
  11. }
  12. return 1;
  13. }
  14. int sum[maxn];
  15. int minn[maxn];
  16. int main(){
  17. cin>>n>>str;
  18. sum[0]=0;
  19. for(int i=0;i<n;i++){
  20. if(str[i]=='(') sum[i+1]=sum[i]+1;
  21. else if(str[i]==')') sum[i+1]=sum[i]-1;
  22. }
  23. minn[n]=sum[n];
  24. for(int i=n-1;i>=0;i--){
  25. minn[i]=min(minn[i+1],sum[i]);
  26. }
  27. int ans=0;
  28. for(int i=0;i<n;i++){
  29. if(sum[i]<0) break;
  30. int tmp=sum[i];
  31. if(str[i]==')'){
  32. tmp++;
  33. }else if(str[i]=='('){
  34. tmp--;
  35. }
  36. if(tmp<0) continue;
  37. if(sum[n]-sum[i+1]+tmp!=0) continue;
  38. if(minn[i+1]-sum[i+1]+tmp<0) continue;
  39. ans++;
  40. }
  41. cout<<ans<<endl;
  42. }

F. Make It Connected

传送门

题意:给你一个无向图,有n个点,每个点有一个权值,从a点走到b点的花费是a、b的权值和,有m条边可以连接,如果连接u和v则花费w的权值,当然也可以选择不连,求使得这个图联通的最小花费

题解:我们找到一个起点,要想使得这个生成这个图的花费最小,那么起点一定是权值最小的那个,连边时将这个起点和所有的点连接起来,然后最后跑一个最小生成树即可

代码

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <stack>
  6. #include <queue>
  7. #include <cstdio>
  8. #include <cctype>
  9. #include <bitset>
  10. #include <string>
  11. #include <vector>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <functional>
  16. #define PI acos(-1)
  17. #define eps 1e-8
  18. #define fuck(x) cout<<#x<<" = "<<x<<endl;
  19. #define lson l,mid,rt<<1
  20. #define rson mid+1,r,rt<<1|1
  21. #define FIN freopen("input.txt","r",stdin);
  22. #define FOUT freopen("output.txt","w+",stdout);
  23. //#pragma comment(linker, "/STACK:102400000,102400000")
  24. using namespace std;
  25. typedef long long LL;
  26. typedef unsigned long long ull;
  27. typedef pair<int, int> PII;
  28. const int maxn = 3e5 + 5;
  29. const LL INF = 1e18 + 7;
  30. const ull mod = 9223372034707292160;
  31. LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
  32. LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
  33. 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;}
  34. 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;}
  35. int n, m;
  36. LL a[maxn];
  37. struct EDGE {
  38. int u, v;
  39. LL w;
  40. bool operator < (const EDGE&a) {
  41. return w < a.w;
  42. }
  43. }edge[maxn<<2];
  44. int f[maxn];
  45. int find(int x){
  46. return x==f[x]?x:f[x]=find(f[x]);
  47. }
  48. int main() {
  49. #ifndef ONLINE_JUDGE
  50. FIN
  51. #endif
  52. cin>>n>>m;
  53. int minn=0;
  54. a[0]=INF;
  55. for(int i=1;i<=n;i++){
  56. scanf("%lld",&a[i]);
  57. if(a[i]<a[minn]) minn=i;
  58. }
  59. for(int i=1;i<=n;i++){
  60. edge[i]=(EDGE){i,minn,a[i]+a[minn]};
  61. f[i]=i;
  62. }
  63. for(int i=n+1;i<=n+m;i++){
  64. scanf("%d%d%lld",&edge[i].u,&edge[i].v,&edge[i].w);
  65. }
  66. sort(edge+1,edge+1+n+m);
  67. LL ans=0;
  68. for(int i=1;i<=n+m;i++){
  69. int u=find(edge[i].u);
  70. int v=find(edge[i].v);
  71. if(u!=v){
  72. f[u]=v;
  73. ans+=edge[i].w;
  74. }
  75. }
  76. cout<<ans<<endl;
  77. }

# Codeforces Round #529(Div.3)个人题解的更多相关文章

  1. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  2. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  3. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  4. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  5. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  6. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

  7. Codeforces Round #499 (Div. 1)部分题解(B,C,D)

    Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...

  8. Codeforces Round #545 (Div. 1) 简要题解

    这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...

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

    Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...

随机推荐

  1. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  2. String和StringBuffer以及StringBuilder的区别

    今天在读<java编程思想>的时间,在看到String和StringBuffer以及StringBuffer这三个类的时间,做一个随笔小结,为自己的面试做好准备! 一:String,Str ...

  3. Microsoft Edge 浏览器开始支持webkit私有样式

    微软表示新版的浏览器Edge(spartan)不会再增加新的私有属性,同时移除了部分-ms-属性,但很多标准在没有支持到之前,会使用webkit的api.Edge开发工程师Jacob Rossi列出了 ...

  4. Memcache的客户端连接系列(二) Python

    关键词: Memcached   Python 客户端 声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(Memcached)的用户指南.客户端连接方法通用,故摘抄过来分享给大家. Python ...

  5. Leetcode - 557. Reverse Words in a String III (C++) stringstream

    1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...

  6. 关于jquery几个自己不咋用到的常用遍历赛选的api

    1.contains:作用是返回包含某个文字的元素节点 例子:要给所以含有“lyz”的p节点加样式: 可以这样:$("p:contains(lyz)").css("col ...

  7. 软件功能说明书——Thunder团队

    爱阅APP功能说明书 一.引言 相信大家都使用过电子书阅读器,相对于纸质版书籍电子书APP做到了环保.易存储.便携.因此我们Thunder团队开发了——爱阅APP,以下内容是Alpha版的功能说明书. ...

  8. Java学习个人备忘录之关键字final

    final关键字final可以修饰类,方法,变量.final修饰的类不可以被继承final修饰的方法不可以被覆盖final修饰的变量是一个常量.只能被赋值一次.内部类只能访问被final修饰的局部变量 ...

  9. C#操作Excel执行分类多条件汇总合并

    之前发了一片模拟合并,详见模拟Excel同一列相同值的单元格合并 在之前的文章中介绍了思想,其中Excel采用的二维数组模拟,今天花了点时间,学习了一下C#操作Excel,实现了类似的效果! 准备 需 ...

  10. .net 简体转换繁体实例,繁体转换简体 Encode.dll、下载

    在项目中先引用Encode.dll  下面是下载地址: Encode.dll ChineseConverter.dll 1.html页面代码 <%@ Page Language="C# ...