Codeforces Beta Round #75 (Div. 2 Only)
http://codeforces.com/contest/92
A
1 #include<iostream> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 100005 7 typedef long long ll; 8 typedef unsigned long long ull; 9 const ull MOD=257; 10 /*#ifndef ONLINE_JUDGE 11 freopen("1.txt","r",stdin); 12 #endif */ 13 14 15 16 int main(){ 17 #ifndef ONLINE_JUDGE 18 // freopen("1.txt","r",stdin); 19 #endif 20 std::ios::sync_with_stdio(false); 21 int n,m; 22 cin>>n>>m; 23 int i=0; 24 while(i+1<=m){ 25 m-=i+1; 26 i++; 27 if(i==n) i%=n; 28 } 29 cout<<m<<endl; 30 }
B
模拟+找规律
1 #include<iostream> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 100005 7 typedef long long ll; 8 typedef unsigned long long ull; 9 const ull MOD=257; 10 /*#ifndef ONLINE_JUDGE 11 freopen("1.txt","r",stdin); 12 #endif */ 13 14 ///1 15 16 int main(){ 17 #ifndef ONLINE_JUDGE 18 // freopen("1.txt","r",stdin); 19 #endif 20 std::ios::sync_with_stdio(false); 21 string str; 22 cin>>str; 23 if(str=="1"){ 24 cout<<0; 25 return 0; 26 } 27 int ans=0; 28 for(int i=str.length()-1;i>=0;i--){ 29 if(str[i]=='1'&&i==0) break; 30 if(str[i]=='1'){ 31 ans+=2; 32 int l=i; 33 while(l>=0&&str[l]=='1'){ 34 str[l]='0'; 35 l--; 36 } 37 if(l>=0) str[l]='1'; 38 } 39 else{ 40 ans++; 41 } 42 43 } 44 cout<<ans<<endl; 45 }
C
二分
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define eb emplace_back 8 #define maxn 1000006 9 #define eps 1e-8 10 #define pi acos(-1.0) 11 #define rep(k,i,j) for(int k=i;k<j;k++) 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 typedef pair<char,int> pci; 15 typedef pair<pair<int,string>,pii> ppp; 16 typedef unsigned long long ull; 17 /*#ifndef ONLINE_JUDGE 18 freopen("1.txt","r",stdin); 19 #endif */ 20 21 vector<int>ve[35]; 22 int book[35]; 23 24 int main(){ 25 #ifndef ONLINE_JUDGE 26 // freopen("1.txt","r",stdin); 27 #endif 28 std::ios::sync_with_stdio(false); 29 string s1,s2; 30 cin>>s1>>s2; 31 int ans=1; 32 for(int i=0;i<s1.length();i++){ 33 ve[s1[i]-'a'].pb(i); 34 } 35 int pos=-1; 36 int x,tmp; 37 for(int i=0;i<s2.length();i++){ 38 x=s2[i]-'a'; 39 if(ve[x].size()==0){ 40 cout<<-1<<endl; 41 return 0; 42 } 43 tmp=lower_bound(ve[x].begin(),ve[x].end(),pos+1)-ve[x].begin(); 44 if(tmp!=ve[x].size()){ 45 pos=ve[x][tmp]; 46 } 47 else{ 48 ans++; 49 pos=lower_bound(ve[x].begin(),ve[x].end(),0)-ve[x].begin(); 50 pos=ve[x][pos]; 51 } 52 // cout<<tmp<<" "<<pos<<" "<<ans<<endl; 53 // cout<<pos<<endl; 54 } 55 cout<<ans<<endl; 56 }
D
线段树
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define eb emplace_back 8 #define maxn 100005 9 #define eps 1e-8 10 #define pi acos(-1.0) 11 #define rep(k,i,j) for(int k=i;k<j;k++) 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 typedef pair<char,int> pci; 15 typedef pair<pair<int,string>,pii> ppp; 16 typedef unsigned long long ull; 17 /*#ifndef ONLINE_JUDGE 18 freopen("1.txt","r",stdin); 19 #endif */ 20 21 int a[maxn]; 22 int tree[maxn<<3]; 23 int n; 24 25 void push_up(int rt){ 26 tree[rt]=min(tree[rt<<1],tree[rt<<1|1]); 27 } 28 29 void build(int l,int r,int rt){ 30 if(l==r){ 31 tree[rt]=a[l]; 32 return; 33 } 34 int mid=l+r>>1; 35 build(lson); 36 build(rson); 37 push_up(rt); 38 } 39 40 int query(int L,int R,int v,int l,int r,int rt){ 41 if(l==r&&L<=l&&R>=r){ 42 return l; 43 } 44 int mid=l+r>>1; 45 if(L<=mid&&tree[rt<<1]<v){ 46 return query(L,R,v,lson); 47 } 48 else if(R>mid&&tree[rt<<1|1]<v){ 49 return query(L,R,v,rson); 50 } 51 return -1; 52 } 53 54 int main(){ 55 #ifndef ONLINE_JUDGE 56 // freopen("1.txt","r",stdin); 57 #endif 58 std::ios::sync_with_stdio(false); 59 cin>>n; 60 for(int i=n;i>=1;i--) cin>>a[i]; 61 build(1,n,1); 62 int pos; 63 vector<int>ans; 64 for(int i=1;i<=n;i++){ 65 pos=query(1,i,a[i],1,n,1); 66 if(pos==-1) ans.pb(pos); 67 else ans.pb(i-pos-1); 68 } 69 reverse(ans.begin(),ans.end()); 70 for(int i=0;i<ans.size();i++) cout<<ans[i]<<" "; 71 }
E
并查集
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define eb emplace_back 8 #define maxn 100005 9 #define eps 1e-8 10 #define pi acos(-1.0) 11 #define rep(k,i,j) for(int k=i;k<j;k++) 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 typedef pair<char,int> pci; 15 typedef pair<pair<int,string>,pii> ppp; 16 typedef unsigned long long ull; 17 const long long MOD=1e9+9; 18 /*#ifndef ONLINE_JUDGE 19 freopen("1.txt","r",stdin); 20 #endif */ 21 22 int n,m; 23 int fa[100005]; 24 25 int Find(int x){ 26 int r=x,y; 27 while(x!=fa[x]) x=fa[x]; 28 while(r!=x){ 29 y=fa[r]; 30 fa[r]=x; 31 r=y; 32 } 33 return x; 34 } 35 36 int main(){ 37 #ifndef ONLINE_JUDGE 38 // freopen("1.txt","r",stdin); 39 #endif 40 std::ios::sync_with_stdio(false); 41 cin>>n>>m; 42 int x,y,xx,yy; 43 ll ans=1; 44 for(int i=0;i<=n;i++) fa[i]=i; 45 for(int i=0;i<m;i++){ 46 cin>>x>>y; 47 xx=Find(x),yy=Find(y); 48 if(xx==yy){ 49 ans=(ans+ans)%MOD; 50 } 51 else fa[xx]=yy; 52 cout<<ans-1<<endl; 53 } 54 55 }