Codeforces Round #672 (Div. 2)
身败名裂
A B C D
英语阅读题。
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=5e4+11; int T,N,A[MAXN]; int main(){ T=read(); while(T--){ N=read();for(int i=1;i<=N;i++) A[i]=read(); bool ff=1;for(int i=2;i<=N;i++) if(A[i-1]<=A[i]) ff=0; if(!ff) printf("YES\n"); else printf("NO\n"); }return 0; }
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define int long long using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=1e5+11; int T,N,A[MAXN],cnt[61],Ans; signed main(){ //freopen("1.in","r",stdin); T=read(); while(T--){ memset(cnt,0,sizeof(cnt)); N=read();for(int i=1;i<=N;i++) A[i]=read(); int ps=1;Ans=0; for(int i=1;i<=N;i++){ int pw=1; for(int j=0;;j++){ //cerr<<"j:"<<j<<" "<<pw<<endl; if(A[i]<pw){cnt[j-1]++;break;} pw*=2; } } for(int i=0;i<=60;i++) Ans+=(cnt[i]*(cnt[i]-1))/2; printf("%lld\n",Ans); }return 0; }
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define int long long using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=3e5+11; int T,N,d[MAXN],A[MAXN],Ans,Q; void Modify(int ps,int w){ if(d[ps]>=0) Ans-=d[ps]; d[ps]=w-A[ps+1]; if(d[ps]>=0) Ans+=d[ps]; if(ps!=1){ if(d[ps-1]>=0) Ans-=d[ps-1]; d[ps-1]=A[ps-1]-w; if(d[ps-1]>=0) Ans+=d[ps-1]; } A[ps]=w;return; } signed main(){ //freopen("1.in","r",stdin); T=read(); while(T--){ N=read(),Q=read();for(int i=1;i<=N;i++) A[i]=read();A[N+1]=0; Ans=0;for(int i=1;i<=N;i++) d[i]=(A[i]-A[i+1]),Ans+=max(0ll,d[i]); printf("%lld\n",Ans); while(Q--){ int l=read(),r=read(); int w1=A[l],w2=A[r]; Modify(l,w2); Modify(r,w1); printf("%lld\n",Ans); } }return 0; }
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define int long long #define mod 998244353 using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=6e5+11; struct Segment{ int Ans[MAXN<<2]; void build(int k,int l,int r){ Ans[k]=0;if(l==r) return; int mid=(l+r)>>1;build(k<<1,l,mid),build(k<<1|1,mid+1,r); return; } void Modify(int k,int l,int r,int ps){ if(l==r){Ans[k]++;return;} int mid=(l+r)>>1;if(ps<=mid) Modify(k<<1,l,mid,ps);else Modify(k<<1|1,mid+1,r,ps); Ans[k]=Ans[k<<1]+Ans[k<<1|1]; } int Query(int k,int l,int r,int x,int y){ if(x<=l&&r<=y) return Ans[k]; int mid=(l+r)>>1,res=0; if(x<=mid) res+=Query(k<<1,l,mid,x,y); if(mid<y) res+=Query(k<<1|1,mid+1,r,x,y); return res; } }S; int T,N,K,tmp[MAXN<<1],M,Ans,fac[MAXN],ifac[MAXN],inv[MAXN],L[MAXN],R[MAXN]; pii p[MAXN]; int C(int a,int b){if(a<b) return 0;return fac[a]*ifac[b]%mod*ifac[a-b]%mod;} signed main(){ //freopen("1.in","r",stdin); fac[0]=fac[1]=inv[1]=ifac[0]=ifac[1]=1; for(int i=2;i<MAXN;i++) fac[i]=fac[i-1]*i%mod,inv[i]=(mod-mod/i)*inv[mod%i]%mod,ifac[i]=ifac[i-1]*inv[i]%mod; T=1; while(T--){ tmp[0]=0; N=read(),K=read();Ans=0;for(int i=1;i<=N;i++) L[i]=read(),R[i]=read(),tmp[++tmp[0]]=L[i],tmp[++tmp[0]]=R[i]; sort(tmp+1,tmp+tmp[0]+1);M=unique(tmp+1,tmp+tmp[0]+1)-tmp-1; for(int i=1;i<=N;i++) p[i].fi=lower_bound(tmp+1,tmp+M+1,L[i])-tmp,p[i].se=lower_bound(tmp+1,tmp+M+1,R[i])-tmp; sort(p+1,p+N+1);S.build(1,1,M); for(int i=1;i<=N;i++){ int res=S.Query(1,1,M,p[i].fi,M); Ans+=C(res,K-1);Ans%=mod; S.Modify(1,1,M,p[i].se); }printf("%lld\n",Ans); }return 0; }
E
$O(n^5)$ 可过?
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #include<climits> #include<bitset> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=82; int f[MAXN][MAXN][MAXN*MAXN],A[MAXN],P[MAXN],N,M,Ans,tot; int calc(int x){return (x*(x-1))/2;} int main(){ //freopen("1.in","r",stdin); N=read();for(int i=1;i<=N;i++) A[i]=read();M=(N*(N-1))/2; for(int i=1;i<=N;i++) if(A[i]) P[++tot]=i;Ans=calc(N-tot);P[++tot]=N+1; memset(f,-127/3,sizeof(f));f[0][0][0]=0; for(int i=0;i<tot;i++){ for(int j=i;j<=N+1;j++){ for(int x=j+1;x<=N+1;x++){ int dis=abs(P[i+1]-x),ww=calc(x-j-1); for(int pp=0;pp<=M;pp++) if(pp+dis<=M){ f[i+1][x][pp+dis]=max(f[i+1][x][pp+dis],f[i][j][pp]-ww); } } } } for(int i=0;i<=M;i++) {printf("%d ",Ans+f[tot][N+1][i]),f[tot][N+1][i+1]=max(f[tot][N+1][i+1],f[tot][N+1][i]);} printf("\n"); return 0; }
$O(n^4\log n)$ 的斜率优化做法 $dyls$ 的题解。 link
还有更加厉害的做法 link