2018icpc青岛
C 分类讨论+思路
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+10; string s,t; int a[N]; vector<int> num; #define mp make_pair #define p pair<int,int> vector<p> st; int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--){ st.clear(); ll n; cin>>n; cin>>s>>t; s=" "+s; t=" "+t; for(int i=1;i<=n;i++)if(s[i]!=t[i])a[i]=1;else a[i]=0; a[n+1]=0; int l=-1,r=-1; for(int i=1;i<=n+1;i++){ if(a[i]==1&&a[i-1]!=1){ l=i,r=i; }else if(a[i]==1&&a[i-1]==1)r=i; else if(a[i]==0){ if(l!=-1&&r!=-1) st.push_back(mp(l,r)); l=-1,r=-1; } } if(st.size()==0){ cout<<n*(n+1)/2<<endl; }else if(st.size()==1){ ll l=st[0].first-1; ll r=n-st[0].second; ll op=st[0].second-st[0].first+1; cout<<l*2+r*2+(op-1ll)*2<<endl; }else if(st.size()==2){ cout<<6<<endl; }else{ cout<<0<<endl; } } return 0; }
D 枚举首位,后面都是固定的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10; char C[N]; int a[N],b[N],c[N]; int main() { //ios::sync_with_stdio(false); int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d%s",&n,&m,C); for(int i=0; C[i]!='\0';i++) c[i]=C[i]-48; int flag1=0; int l=strlen(C); if(l==1&&c[0]==0) { if(n==1) { printf("0 1"); for(int i=2;i<=m;i++) printf("0"); printf("\n"); } else if(m==1) { printf("1"); for(int i=2;i<=n;i++) printf("0"); printf(" 0\n"); } else printf("Impossible\n"); continue; } for(int i=1; i<=9; i++) { for(int j=1; j<=9; j++) { int flag=1; if(i*j!=c[0]&&(i*j!=c[0]*10+c[1])) continue; a[0]=i; b[0]=j; int k=1,t; if(i*j==c[0]*10+c[1]) t=2; else t=1; while(k<m) { if(t<l&&c[t]%i==0&&c[t]/i<=9) { b[k]=c[t]/i; t++; k++; } else if(t+1<l&&c[t]!=0&&(c[t]*10+c[t+1])%i==0&&(c[t]*10+c[t+1])/i<=9) { b[k]=(c[t]*10+c[t+1])/i; k++; t+=2; } else { flag=0; break; } } if(flag==0) continue; k=1; while(k<n) { if(t<l&&c[t]%j==0&&c[t]/j<=9) { a[k]=c[t]/j; t++; k++; } else if(t+1<l&&c[t]!=0&&(c[t]*10+c[t+1])%j==0&&(c[t]*10+c[t+1])/j<=9) { a[k]=(c[t]*10+c[t+1])/j; k++; t+=2; } else { flag=0; break; } if(flag==0) break; for(int r=1;r<m;r++) { int tmp=a[k-1]*b[r]; if(tmp<10) { if(t<l&&c[t]==tmp) t++; else flag=0; } else { if(c[t]!=0&&t+1<l&&c[t]*10+c[t+1]==tmp) t+=2; else flag=0; } if(flag==0) break; } if(flag==0) break; } if(flag!=0&&t==l) { flag1=1; break; } } if(flag1==1) break; } if(flag1==0) printf("Impossible\n"); else { for(int i=0; i<n; i++) printf("%d",a[i]); printf(" "); for(int i=0; i<m; i++) printf("%d",b[i]); printf("\n"); } } return 0; }
E 二分答案,check的时候一定是一个点一个点走
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10; ll n,m; ll a[N]; ll s[N]; bool check(ll x){ int i; for(i=1;i<=n;i++){ if(x%a[i]){ s[i]=x/a[i]+1; } else{ s[i]=x/a[i]; } } ll step=0; for(int i=1;i<n;i++){ step++; s[i]--; if(s[i]<=0) continue; step+=2*s[i]; s[i+1]-=s[i]; if(step>m) return false; } if(s[n]>0){ step++; s[n]--; step+=s[n]*2; } if(step>m) return false; return true; } int main(){ ios::sync_with_stdio(false); int t; cin>>t; while(t--){ cin>>n>>m; int i; for(i=1;i<=n;i++) cin>>a[i]; if(m==0){ cout<<0<<endl; continue; } ll l=0,r=1e18; while(l<r){ ll mid=l+r+1>>1; if(check(mid)) l=mid; else r=mid-1; } cout<<l<<endl; } return 0; }
I solider game(线段树)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pll; const int N=2e5+10; pll p[N<<2]; int a[N][2]; int tr[N<<2][2][2]; int n; bool cmp(pll x,pll y){ return a[x.first][x.second]<a[y.first][y.second]; } void build(int u,int l,int r){ tr[u][0][0]=tr[u][1][0]=tr[u][1][1]=tr[u][0][1]; if(l==r){ tr[u][1][0]=1; return ; } int mid=l+r>>1; build(u<<1,l,mid); build(u<<1|1,mid+1,r); } void pushup(int rt){ tr[rt][0][0] = (tr[rt<<1][0][0] && tr[rt<<1|1][0][0]) || (tr[rt<<1][0][1] && tr[rt<<1|1][1][0]); tr[rt][0][1] = (tr[rt<<1][0][0] && tr[rt<<1|1][0][1]) || (tr[rt<<1][0][1] && tr[rt<<1|1][1][1]); tr[rt][1][0] = (tr[rt<<1][1][0] && tr[rt<<1|1][0][0]) || (tr[rt<<1][1][1] && tr[rt<<1|1][1][0]); tr[rt][1][1] = (tr[rt<<1][1][1] && tr[rt<<1|1][1][1]) || (tr[rt<<1][1][0] && tr[rt<<1|1][0][1]); } void modify(int u,int l,int x,int L,int R){ if(L==R){ tr[u][0][x]^=1; return ; } int mid=L+R>>1; if(l<=mid) modify(u<<1,l,x,L,mid); else modify(u<<1|1,l,x,mid+1,R); pushup(u); } int main(){ ios::sync_with_stdio(false); int t; cin>>t; while(t--){ int cnt=0; cin>>n; int i; for(i=1;i<=n;i++){ cin>>a[i][0]; p[++cnt]={i,0}; if(i-1){ a[i-1][1]=a[i-1][0]+a[i][0]; p[++cnt]={i-1,1}; } } sort(p+1,p+cnt+1,cmp); build(1,1,n); int j; ll ans=1e18; for(i=1,j=1;i<=cnt;i++){ while(j<=cnt&&!tr[1][0][0]){ modify(1,p[j].first,p[j].second,1,n); j++; } if(tr[1][0][0]){ ans=min(ans,1ll*a[p[j-1].first][p[j-1].second]-1ll*a[p[i].first][p[i].second]); } modify(1,p[i].first,p[i].second,1,n); } cout<<ans<<endl; } return 0; }
J 贪心
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5; ll a[N]; int main(){ int t;scanf("%d",&t); while(t--){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); if(a[i]==0) { n--; m--; i--; } } if(n==m) { printf("Richman\n"); continue; } if(m<0||m>n) { printf("Impossible\n"); continue; } ll ans=0; for(int i=1;i<=m;i++) ans+=a[i]; ll mi=1e18; for(int i=m+1;i<=n;i++) { mi=min(a[i],mi); } ans+=(mi-1); printf("%lld\n",ans); } return 0; }
M 签到
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; int g[10]={1,0,0,0,1,0,1,0,2,1}; int f(int n){ int sum=0; if(n==0)return 1; while(n){ int x=n%10; n/=10; sum+=g[x]; } return sum; } int main(){ int t;scanf("%d",&t); while(t--){ int n,k; cin>>n>>k; int s=k; if(k>=20){ s=20; while(s--)n=f(n); if((k-20)%2==0)cout<<n<<endl; else cout<<(n^1)<<endl; } else { while(s--)n=f(n);cout<<n<<endl; } } }
没有人不辛苦,只有人不喊疼