620A - Professor GukiZ‘s Robot 20171122
\(ans=max(\left | x2-x1 \right |,\left | y2-y1 \right |)\)
#include<stdlib.h> #include<stdio.h> #include<math.h> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int X[2],Y[2],x,y; int main() { scanf("%d%d%d%d",&X[0],&Y[0],&X[1],&Y[1]); x=abs(X[1]-X[0]),y=abs(Y[1]-Y[0]); printf("%d\n",max(x,y)); return 0; }
620B - Grandfather Dovlet’s calculator 20171122
预处理每个字符的花费,按照题意模拟即可
#include<stdlib.h> #include<stdio.h> #include<math.h> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int ans,a,b,f[10]; int cal(int k) { int _=0; while(k) _+=f[k%10],k/=10; return _; } int main() { scanf("%d%d",&a,&b); f[0]=6,f[1]=2,f[2]=5,f[3]=5,f[4]=4; f[5]=5,f[6]=6,f[7]=3,f[8]=7,f[9]=6; for(int i=a;i<=b;i++) ans+=cal(i); printf("%d\n",ans); return 0; }
620C - Pearls in a Row 20171122
开个set记录当前已在区间类的珍珠种类,O(n)扫一遍即可
#include<stdlib.h> #include<stdio.h> #include<math.h> #include<set> #include<vector> #include<cstring> #include<iostream> #include<algorithm> using namespace std; vector<int>l,r; int n,x,t=1; set<int>s; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { if(t==i)s.clear(); scanf("%d",&x); if(s.count(x)) l.push_back(t),r.push_back(i),t=i+1; else s.insert(x); } if(!l.size())return printf("-1\n"),0; r[r.size()-1]=n; printf("%d\n",l.size()); for(int i=0;i<l.size();i++) printf("%d %d\n",l[i],r[i]); return 0; }
620D - Professor GukiZ and Two Arrays 20171122
暴力分类讨论\(k=0,1,2\)的所有情况就好了
#include<stdlib.h> #include<stdio.h> #include<math.h> #define N 2001 #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define LL long long struct rua { LL i,j,v; }f[N*N]; LL n,m,d,sa,sb,a[N],b[N],ans[3],v[N*N],_,a1,b1,b2; bool cmp(rua x,rua y){return x.v<y.v;} void cdx(LL i,LL _) { if(!_)return; if(f[_].i==f[i].i || f[_].j==f[i].j)return; if(ans[2]>abs(d+v[_])) ans[2]=abs(d+v[_]),b1=i,b2=_; } int main() { ans[2]=(1ll<<62); scanf("%I64d",&n); for(LL i=1;i<=n;i++) scanf("%I64d",&a[i]),sa+=a[i]; scanf("%I64d",&m); for(LL i=1;i<=m;i++) scanf("%I64d",&b[i]),sb+=b[i]; d=sa-sb; for(LL i=1;i<=n;i++) for(LL j=1;j<=m;j++) { f[i*m-m+j].v=2*(b[j]-a[i]); f[i*m-m+j].i=i; f[i*m-m+j].j=j; } ans[0]=abs(d); sort(f+1,f+n*m+1,cmp); for(LL i=1;i<=n;i++) for(LL j=1;j<=m;j++) v[i*m-m+j]=f[i*m-m+j].v; _=lower_bound(v+1,v+n*m+1,-d)-v; if(_==n*m+1)ans[1]=abs(d+v[_-1]),a1=_-1;else if(_==1)ans[1]=abs(d+v[1]),a1=_;else if(abs(d+v[_])<abs(d+v[_-1]))ans[1]=abs(d+v[_]),a1=_; else ans[1]=abs(d+v[_-1]),a1=_-1; for(int i=1;i<n*m;i++) { d+=v[i]; _=lower_bound(v+1,v+n*m+1,-d)-v; if(_==n*m+1)cdx(i,_-1); else if(_==1)cdx(i,1-(i==1));else cdx(i,_+(i==_)),cdx(i,_-1-(i==_-1)); d-=v[i]; } if(ans[0]<=ans[1] && ans[0]<=ans[2]) return printf("%I64d\n0\n",ans[0]),0; if(ans[1]<=ans[0] && ans[1]<=ans[2]) return printf("%I64d\n1\n%I64d %I64d\n",ans[1],f[a1].i,f[a1].j),0; printf("%I64d\n2\n",ans[2]); printf("%I64d %I64d\n",f[b1].i,f[b1].j); printf("%I64d %I64d\n",f[b2].i,f[b2].j); return 0; }
620E - New Year Tree 20180919
预处理每个点的DFS序,可以得出每棵子树对应的DFS序的范围。由于\(c_i\)不超过60,故可将修改操作转换为:把子树内所有点的价值改为\(2^{c}\),将询问操作转换为:询问子树内所有点价值进行或运算的结果在二进制中1的个数,用线段树做就好了
#include<bits/stdc++.h> using namespace std; #define N 400001 #define LL long long vector<int>d[N]; int n,m,t,x,y,c[N],l[N],r[N]; struct rua{int l,r;LL f,w;}tr[N<<2]; LL a[N]; void update(int x) { int lson=x*2,rson=x*2+1; tr[x].w=tr[lson].w|tr[rson].w; } void down(int x) { LL c=tr[x].w;tr[x].f=0; int lson=x*2,rson=x*2+1; tr[lson].f=tr[lson].w=c; tr[rson].f=tr[rson].w=c; } void Build(int l,int r,int x) { tr[x]={l,r,0,0}; if(l==r){tr[x].w=a[l];return;} int mid=l+r>>1; Build(l,mid,x*2); Build(mid+1,r,x*2+1); update(x); } void Change(int L,int R,LL c,int x) { int l=tr[x].l,r=tr[x].r; int mid=l+r>>1; if(L<=l && r<=R){tr[x].f=tr[x].w=c;return;} if(tr[x].f)down(x); if(L<=mid)Change(L,R,c,x*2); if(R>mid)Change(L,R,c,x*2+1); update(x); } LL Query(int L,int R,int x) { int l=tr[x].l,r=tr[x].r; int mid=l+r>>1; LL res=0; if(L<=l && r<=R)return tr[x].w; if(tr[x].f)down(x); if(L<=mid)res|=Query(L,R,x*2); if(R>mid)res|=Query(L,R,x*2+1); update(x); return res; } void dfs(int cur,int pre) { l[cur]=++x; a[x]=1ll<<c[cur]; for(auto nxt:d[cur])if(nxt!=pre)dfs(nxt,cur); r[cur]=x; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&c[i]); for(int i=2;i<=n;i++) scanf("%d%d",&x,&y), d[x].push_back(y), d[y].push_back(x); x=0,dfs(1,0); Build(1,n,1); for(int i=1;i<=m;i++) { scanf("%d",&t); if(t==1) scanf("%d%d",&x,&y), Change(l[x],r[x],1ll<<y,1); else scanf("%d",&x), printf("%d\n",(int)__builtin_popcountll(Query(l[x],r[x],1))); } }
620F - Xors on Segments 20171122
这题...预处理从1异或到n的值(即前缀异或和),\(O(n^{2})\)莽一下就过了...CF的评测机是跑得真快
#include<stdlib.h> #include<stdio.h> #include<math.h> #define N 50001 #define M 1000001 #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,m,a[N],l[N],r[N],t[N],ans[N],f[M]; int main() { for(int i=1;i<M;i++) f[i]=f[i-1]^i; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=m;i++) scanf("%d%d",&l[i],&r[i]); for(int i=1;i<=n;i++) { int _=0;t[i-1]=0; for(int j=i;j<=n;j++) _=max(_,f[a[i]]^f[a[j]]^min(a[i],a[j])),t[j]=_; for(int j=1;j<=m;j++) if(l[j]<=i && r[j]>=i) ans[j]=max(ans[j],t[r[j]]); } for(int i=1;i<=m;i++)printf("%d\n",ans[i]);return 0; }
原文地址:https://www.cnblogs.com/DeaphetS/p/9673982.html
时间: 2024-10-10 16:37:38