1. 程式人生 > >Codeforces 1159D The minimal unique substring(構造)

Codeforces 1159D The minimal unique substring(構造)

erl ext style unsigned cond inf fabs 發現 這一

首先我們先觀察三個串 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

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

Codeforces 1159D The minimal unique substring(構造)