2018年全国多校算法寒假训练营练习比赛(第五场)
A 逆序数
归并求逆序数标打
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e6 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) int a[sz],t[sz]; ll n,sum; void merge_func(int s1,int e1,int s2,int e2) { int p=s1,p1=s1,p2=s2; while(p1<=e1&&p2<=e2) { if(a[p1]<=a[p2]) t[p++]=a[p1++]; else t[p++]=a[p2++],sum+=(e1-p1+1); } while(p1<=e1) t[p++]=a[p1++]; while(p2<=e2) t[p++]=a[p2++]; f(i,s1,e2) a[i]=t[i]; } void merge(ll l,ll r) { if(l<r) { ll mid =(l+r)>>1; merge(l,mid); merge(mid+1,r); merge_func(l,mid,mid+1,r); } } int main() { freopen("in","r",stdin); freopen("out","w",stdout); cin>>n; f(i,1,n)cin>>a[i]; merge(1,n); cout<<sum<<endl; return 0; }
维护一下前缀和就好了
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e6 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) int n,m; int a[sz];ll b[sz]; ll s[sz]; int op,l,r; int main() { freopen("in","r",stdin); freopen("out","w",stdout); cin>>n>>m; f(i,1,n)cin>>a[i],s[i]=s[i-1]+a[i]; f(j,1,m) { cin>>op>>l>>r; if(op==1) { f(i,l,n)s[i]+=r; } else { cout<<s[r]-s[l-1]<<endl; } } return 0; }
C 字符串的问题
只过了50% ,没找到错误,帮我改一下 ,kmp 取一下nxt 数组 然后最后一个元素的大小 几位前缀后缀相同
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e6 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); char a[sz]; int nxt[sz],nn[sz]; void getnxt() { int i=0,len=strlen(a),k=-1; CLR(nxt,-1); while(i<len) { if(k==-1||a[i]==a[k]) nxt[++i]=++k; else k=nxt[k]; } } void kmp() { getnxt(); int len=strlen(a); int ans =nxt[len-1]; if(ans==0) { cout<<"Just a legend"<<endl; return ; } bool flag =false; g(i,len-2,0) if(ans==nxt[i]) { flag=true; break; } if(flag)f(i,0,ans)cout<<a[i]; else cout<<"Just a legend"<<endl; // f(i,0,len-1)cout<<nxt[i]<<" "; /* f(i,0,len-1)ans=max(ans,nxt[i]); f(i,0,len-1) nn[nxt[i]]++; f(i,ans,1) if(nn[i]==2)break; else ans=i-1; if(ans<2)cout<<"Just a legend"<<endl; else cout<<ans<<endl; */ } int main() { FAST_IO ; freopen("in","r",stdin); freopen("out","w",stdout); cin>>a; kmp(); return 0; }
D 集合问题
求一下交并集 ,然后合并 一下就好了
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e6 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); int n,a,b; int p[sz],fa[sz]; map<int,int> m; int Find(int x) { int r=x,t; while(r!=fa[r])r=fa[r]; while(x!=r) { t=fa[x]; fa[x]=r; x=t; } return r; } void Union(int u,int v){ int uu=Find(u); int vv=Find(v); if(uu!=vv) fa[uu]=vv; } void work() { cin>>n>>a>>b; int mm=0; f(i,1,n) { cin>>p[i]; m[p[i]]=i; mm = max(mm,p[i]); } if(mm >max(a,b)) { cout<<"NO"<<endl; return ; } f(i,0,n+1)fa[i]=i; f(i,1,n) { if(m[a-p[i]])Union(i,m[a-p[i]]); else Union(i,n+1); if(m[b-p[i]])Union(i,m[b-p[i]]); else Union(i,0); } int A =Find(0); int B = Find(n+1); if(A!=B) { cout<<"YES"<<endl; f(i,1,n) { if(i!=1) cout<<" "; if(Find(i)==A)cout<<0; else cout<<1; } } else cout<<"NO"<<endl; return ; } int main() { FAST_IO ; freopen("in","r",stdin); freopen("out","w",stdout); work(); return 0; }
E 情人节的电灯泡
树状数组,直接报了的话 TLE
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e4 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); #define pii pair<int,int> #define lowbit(x) x&(-x) #define X first #define Y second int n,m,op,s1,e1,s2,e2; int bits[sz][sz],a[sz][sz]; void update(int l,int r,int k) { for(int i=l;i<=n;i+=lowbit(i)) for(int j=r;j<=n;j+=lowbit(j)) bits[i][j]+=k; } int query(int l,int r) { int ans=0; for(int i=l;i;i-=lowbit(i)) for(int j=r;j;j-=lowbit(j)) ans+=bits[i][j]; return ans; } void work() { cin>>n>>m; f(i,1,n)f(j,1,n) { cin>>a[i][j]; if(a[i][j])update(i,j,1); } f(i,1,m) { cin>>op; if(op==1) { cin>>s1>>e1; int v=(a[s1][e1]^1)-a[s1][e1]; a[s1][e1]^=1; update(s1,e1,v); } else { cin>>s1>>e1>>s2>>e2; cout<<query(s2,e2)-query(s1-1,e2)-query(s2,e1-1)+query(s1-1,e1-1)<<endl; } } } int main() { FAST_IO ; freopen("in","r",stdin); freopen("out","w",stdout); work(); return 0; }
斐波那契博弈论 ,斐波那契数列的数字都是输家
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e6 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); #define pii pair<int,int> #define lowbit(x) x&(-x) #define X first #define Y second int calc(int n) { if(n<10)return n; int ans = 0; while(n) { ans+=n%10; n/=10; } return calc(ans); } void work() { int n; cin>>n; cout<<calc(n)<<endl; } int main() { FAST_IO ; freopen("in","r",stdin); freopen("out","w",stdout); work(); cout<<"hello"<<endl; return 0; }
G 送分啦-QAQ
和前几天一场cf 题目一样
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e6 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); #define pii pair<int,int> #define lowbit(x) x&(-x) #define X first #define Y second void work() { int n,a=1,b=1,s; bool flag=false; cin>>n; while(a<n) { s=b; b=a+b; a=s; } if(a==n)cout<<"Sha"<<endl; else cout<<"Xian"<<endl; } int main() { FAST_IO ; freopen("in","r",stdin); freopen("out","w",stdout); work(); return 0; }
暴力了,乱搞出的
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef double dl ; #define INF 0x7f const int inf = 987654321; const int sz = 1e6 + 5; const int mod = 1e9 + 7; const int sqrtn = 300; #define f(i,l,r) for(int i=l;i<=r;++i) #define g(i,l,r) for(int i=l;i>=r;--i) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); #define pii pair<int,int> #define lowbit(x) x&(-x) #define X first #define Y second int calc(int n) { if(n<10)return n; int ans = 0; while(n) { ans+=n%10; n/=10; } return calc(ans); } void work() { int n; cin>>n; cout<<calc(n)<<endl; } int main() { FAST_IO ; freopen("in","r",stdin); freopen("out","w",stdout); work(); cout<<"hello"<<endl; return 0; }
不摸着石头过河,难道要在温柔乡睡到天昏地暗。