Codeforces 1159D The minimal unique substring(构造)

素颜马尾好姑娘i 2021-11-26 11:50 277阅读 0赞

首先我们先观察三个串 1010,110110,11101110,答案都是红色部分,我们可以下一个结论,形如 abab ( a 中有非负整数个 1 , b 中只有一个 0 )这类的字符串答案恒为 2 ,也就是 k==2 ,然后就是用这类字符串去构造出我们所需的 k 。我们可以尝试从末尾加一个1,那么之前的串变成了 10101,1101101,111011101,那么答案为红色部分。我们可以发现,通过我们末尾添加的1,导致之前红色部分的 01 与我们末尾添加的1与前面一个0构成的 01 重复,使得之前的红色部分向后挪一位。于是,我们可以用这一规律去构造出我们想要的k,显然答案就是末尾部分的01(蓝色部分111…10111…10111)满足 0 的个数加 1 的个数等于 k-1 ,那么对中间的影响(绿色部分111…1011111…110111)往后挪一位也就是我们的答案 k ,最后就是算出这形如 abab 字符串 a 部分中的 1 的个数有多少就行了,设 x 为 a 中 1 的个数,方程为 2*x+1+k-1=n ,化简为 x=(n-k)/2 ,根据题意 n 与 k 同奇偶,那么 x 也是唯一确定的,最后构造也由此生成。

  1. 1 // ——By DD_BOND
  2. 2
  3. 3 //#include<bits/stdc++.h>
  4. 4 #include<functional>
  5. 5 #include<algorithm>
  6. 6 #include<iostream>
  7. 7 #include<sstream>
  8. 8 #include<iomanip>
  9. 9 #include<climits>
  10. 10 #include<cstring>
  11. 11 #include<cstdlib>
  12. 12 #include<cstddef>
  13. 13 #include<cstdio>
  14. 14 #include<memory>
  15. 15 #include<vector>
  16. 16 #include<cctype>
  17. 17 #include<string>
  18. 18 #include<cmath>
  19. 19 #include<queue>
  20. 20 #include<deque>
  21. 21 #include<ctime>
  22. 22 #include<stack>
  23. 23 #include<map>
  24. 24 #include<set>
  25. 25
  26. 26 #define fi first
  27. 27 #define se second
  28. 28 #define MP make_pair
  29. 29 #define pb push_back
  30. 30 #define INF 0x3f3f3f3f
  31. 31 #define pi 3.1415926535898
  32. 32 #define lowbit(a) (a&(-a))
  33. 33 #define lson l,(l+r)/2,rt<<1
  34. 34 #define rson (l+r)/2+1,r,rt<<1|1
  35. 35 #define Min(a,b,c) min(a,min(b,c))
  36. 36 #define Max(a,b,c) max(a,max(b,c))
  37. 37 #define debug(x) cerr<<#x<<"="<<x<<"\n";
  38. 38
  39. 39 using namespace std;
  40. 40
  41. 41 typedef long long ll;
  42. 42 typedef pair<int,int> P;
  43. 43 typedef pair<ll,ll> Pll;
  44. 44 typedef unsigned long long ull;
  45. 45
  46. 46 const ll LLMAX=2e18;
  47. 47 const int MOD=1e9+7;
  48. 48 const double eps=1e-8;
  49. 49 const int MAXN=1e6+10;
  50. 50 const int hmod1=0x48E2DCE7;
  51. 51 const int hmod2=0x60000005;
  52. 52
  53. 53 inline ll sqr(ll x){ return x*x; }
  54. 54 inline int sqr(int x){ return x*x; }
  55. 55 inline double sqr(double x){ return x*x; }
  56. 56 ll __gcd(ll a,ll b){ return b==0? a: __gcd(b,a%b); }
  57. 57 ll qpow(ll a,ll n){ll sum=1;while(n){
  58. if(n&1)sum=sum*a%MOD;a=a*a%MOD;n>>=1;}return sum;}
  59. 58 inline int dcmp(double x){ if(fabs(x)<eps) return 0; return (x>0? 1: -1); }
  60. 59
  61. 60 int main(void)
  62. 61 {
  63. 62 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  64. 63 int n,k; cin>>n>>k;
  65. 64 for(int i=0,j=(n-k)/2;i<n;i++){
  66. 65 if(j) cout<<1,j--;
  67. 66 else cout<<0,j=(n-k)/2;
  68. 67 }
  69. 68 return 0;
  70. 69 }

转载于:https://www.cnblogs.com/dd-bond/p/10858347.html

发表评论

表情:
评论列表 (有 0 条评论,277人围观)

还没有评论,来说两句吧...

相关阅读