#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define maxn 1005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> usingnamespace std; voidread(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }usingnamespace Debug; #define fi first #define se second #define mk make_pair constint mod=1e9+7; intpower(int x,int y=mod-2){ int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n; int a[maxn],id[maxn]; int ans[maxn*maxn],cnt; voidcalc(int x){ if (x>n-2) return ; int tmp=a[x+2]; ans[++cnt]=x; a[x+2]=a[x+1];a[x+1]=a[x];a[x]=tmp; } voidsolve(int L){ int i,j; for (i=L;i<=n-2;i++) { int id=i; for (j=i;j<=n;j++) if (a[id]>a[j]) id=j; while (id>i) { if (id==i+1) calc(i),calc(i),id--; elsecalc(id-2),id-=2; } } } signedmain(void){ int i,j,fl=0; read(n); for (i=1;i<=n;i++) read(a[i]); solve(1); if (a[n]>=a[n-1]) { for (printf("%d\n",cnt),i=1;i<=cnt;i++) printf("%d ",ans[i]); return0; } int id=0; for (i=1;i<n;i++) if (a[i]==a[i+1]) id=i; if (id==0) returnputs("-1"),0; int now=i; while (now>id+3) { if (now==id+4) calc(id+3),calc(id+3),now--; elsecalc(now-2),now-=2; } calc(id+1); calc(id); solve(id); if (a[n]>=a[n-1]) { for (printf("%d\n",cnt),i=1;i<=cnt;i++) printf("%d ",ans[i]); return0; } puts("-1"); return0; }
#include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 20005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> usingnamespace std; voidread(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }usingnamespace Debug; #define fi first #define se second #define mk make_pair constint mod=1e9+7; intpower(int x,int y=mod-2){ int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int f[55][maxn],ans; int a[55][maxn],s[55][maxn],suf[maxn]; int n,m,L,q[maxn],head,tail,sf[maxn],pf[maxn]; signedmain(void){ int i,j,k; read(n);read(m);read(L); for (i=1;i<=n;i++) { for (j=1;j<=m;j++) read(a[i][j]),s[i][j]=s[i][j-1]+a[i][j]; } for (j=1;j<=m-L+1;j++) f[1][j]=s[1][j+L-1]-s[1][j-1]; // for (i=2;i<=n;i++) { // for (j=1;j<=m;j++) suf[j]=suf[j-1]+a[i][j]; // for (j=1;j<=m-L+1;j++) { // int res=0; // for (k=1;k<=m-L+1;k++) { // if (k>=j-L+1&&k<=j+L-1) f[i][j]=max(f[i][j],f[i-1][k]+s[i][max(j,k)+L-1]-s[i][min(j,k)-1]); // else f[i][j]=max(f[i][j],f[i-1][k]+s[i][k+L-1]-s[i][k-1]+s[i][j+L-1]-s[i][j-1]); // } // if (i==n) ans=max(ans,f[i][j]); //// gdb(i,j,f[i][j]); // } // }这是暴力,干脆留在这里好了 for (i=2;i<=n;i++) { for (j=1;j<=m;j++) suf[j]=suf[j-1]+a[i][j]; for (j=1;j<=m-L+1;j++) sf[j]=max(sf[j-1],f[i-1][j]+suf[j+L-1]-suf[j-1]); for (j=m-L+1;j>=1;j--) pf[j]=max(pf[j+1],f[i-1][j]+suf[j+L-1]-suf[j-1]); q[head=tail=1]=0; for (j=1;j<=m-L+1;j++) { while (head<=tail&&q[head]<=j-L) head++; while (head<=tail&&f[i-1][q[tail]]-suf[q[tail]-1]<=f[i-1][j]-suf[j-1]) tail--; q[++tail]=j; f[i][j]=max(sf[j-L]+suf[j+L-1]-suf[j-1],f[i-1][q[head]]+suf[j+L-1]-suf[q[head]-1]); f[i][j]=max(f[i][j],pf[j+L]+suf[j+L-1]-suf[j-1]); } q[head=tail=1]=m-L+1; for (j=m-L+1;j>=1;j--) { while (head<=tail&&q[head]>=j+L) head++; while (head<=tail&&f[i-1][q[tail]]+suf[q[tail]+L-1]<=f[i-1][j]+suf[j+L-1]) tail--; q[++tail]=j; f[i][j]=max(f[i][j],f[i-1][q[head]]+suf[q[head]+L-1]-suf[j-1]); } } for (j=1;j<=m-L+1;j++) ans=max(ans,f[n][j]); printf("%lld",ans); return0; }
#include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 200005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> usingnamespace std; voidread(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }usingnamespace Debug; #define fi first #define se second #define mk make_pair constint mod=998244353; intpower(int x,int y=mod-2){ int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n,m,M; #define lowbit(x) ((x)&(-(x))) int a[maxn],g[130000005],pw[18],inv[maxn],h[(1<<17)+5]; double f[(1<<17)+5],ans; voidadd(int &x,int y){x=(x+y)%mod;} signedmain(void){ freopen("ex_match2.in","r",stdin); int i,j,k;char ch; read(n);read(m);M=(1<<m); for (inv[0]=1,i=1;i<=n;i++) inv[i]=power(i); for (pw[0]=1,i=1;i<=m;i++) pw[i]=pw[i-1]*3; for (i=0;i<M;i++) { for (j=0;j<m;j++) if ((i>>j)&1) h[i]+=pw[j]; } for (i=1;i<=n;i++) { for (cin>>ch,j=0;j<m;j++) { if (ch=='1') a[i]|=(1<<j); ch=getchar(); } g[h[M-1]+h[a[i]]]++; } for (i=M-2;i>=0;i--) { int tmp=lowbit((M-1)^i); for (j=(i-1)&i;j>=0;j=i&(j-1)) { g[h[i]+h[j]]=g[h[i+tmp]+h[j]]+g[h[i+tmp]+h[j+tmp]]; if (j==0) break; } } f[M-1]=1; for (i=M-1;i>=0;i--) if (i&1) { int res=0; for (j=(i-1)&i;j;j=i&(j-1)) res+=g[h[i]+h[j]]; if (res==0) ans+=f[i]; else { for (j=(i-1)&i;j;j=i&(j-1)) if (j&1) f[j]+=f[i]*g[h[i]+h[j]]/res; } } printf("%.14lf",ans); return0; }