AtCoder Grand Contest 030题解

cout<<b+min(c,a+b+1);

Tree Burning

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans;ll ret;
int a[200010];
ll hz[200010],qz[200010],qzh[200010];
void calc(){
for(rt i=n-1;i>=1;i--)hz[i]=hz[i+1]+2ll*(n-i)*a[i];
qz[1]=qzh[1]=a[1];
for(rt i=2;i<=n;i++)qz[i]=qz[i-1]+(2ll*i-1)*a[i],qzh[i]=qzh[i-1]+a[i];
ll ans=0;
for(rt i=1;i<=n;i++){
ans+=a[i];int md=i+n>>1;
ret=max(ret,ans-hz[md+1]-(qz[md]-qz[i]-(qzh[md]-qzh[i])*2ll*i)+qzh[n]*(n-i-1));
}
}
int main(){
for(rt i=n;i>=2;i--)a[i]-=a[i-1];
calc();reverse(a+1,a+n+1);calc();
cout<<ret;
return 0;
}

Coloring Torus

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define p 1000000007
#define inv2 500000004
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans[505][505];
int main(){
if(k<=500){
writeln(k);
for(rt i=1;i<=k;i++)for(rt j=1;j<=k;j++)cout<<i<<" \n"[j==k];
return 0;
}
writeln(n=500);
for(rt i=1;i<=n;i++){
ans[1][i]=i;
for(rt x=n,y=i%n+1;x!=1;x--,y=y%n+1)ans[x][y]=i;
}
for(rt i=n+1;i<=k;i++){
if(i+i&1)ans[1][i]=i;
for(rt x=n,y=i%n+1;x!=1;x--,y=y%n+1)if(i+y&1)ans[x][y]=i;
}
for(rt i=1;i<=n;i++)for(rt j=1;j<=n;j++)cout<<ans[i][j]<<" \n"[j==n];
return 0;
}

Inversion Sum

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define p 1000000007
#define inv2 500000004
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans;
int f[3010][3010],g[3010][3010],a[3010];
int main(){
for(rt i=1;i<=n;i++)
for(rt j=1;j<=n;j++)if(a[i]>a[j])f[i][j]=1;
for(rt i=1;i<=m;i++){
for(rt j=1;j<=n;j++)g[x][j]=f[x][j],g[j][x]=f[j][x],g[y][j]=f[y][j],g[j][y]=f[j][y];
f[x][y]=f[y][x]=1ll*(g[x][y]+g[y][x])*inv2%p;
for(rt j=1;j<=n;j++){
const int v1=1ll*(g[x][j]+g[y][j])*inv2%p,v2=1ll*(g[j][x]+g[j][y])*inv2%p;
if(j!=y&&j!=x)f[x][j]=v1,f[j][x]=v2;
if(j!=x&&j!=y)f[y][j]=v1,f[j][y]=v2;
}
}

int ans=0;
for(rt i=1;i<n;i++)
for(rt j=i+1;j<=n;j++)(ans+=f[i][j])%=p;
for(rt i=1;i<=m;i++)ans=ans*2%p;
cout<<ans;
return 0;
}

Less than 3

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans,ret;
int a[100010],b[100010],t1,t2;
char c[100010],s[100010];
int calc(){
t1=t2=0;
for(rt i=1;i<=n;i++)b[++t2]=0;
for(rt i=1;i<n;i++)if(c[i]=='0'&&c[i+1]=='1')a[++t1]=i;
for(rt i=1;i<n;i++)if(s[i]=='0'&&s[i+1]=='1')b[++t2]=i;
for(rt i=1;i<=n;i++)b[++t2]=n;int nowmin=998244353;
for(rt i=1;i<=t2-n+1;i++){
int ans=0;
for(rt j=1;j<i;j++)if(b[j])ans+=b[j];
for(rt j=0;j<t1;j++)ans+=abs(a[j+1]-b[i+j]);
for(rt j=t1;b[i+j]!=n;j++)ans+=n-b[i+j];
nowmin=min(nowmin,ans);
}
return nowmin;
}
int main(){
scanf("%s",c+1);scanf("%s",s+1);
if(n==1&&c[1]!=s[1])writeln(1);
else if(n==2&&c[1]!=s[1]&&c[1]==c[2]&&s[1]==s[2])writeln(2);
else {
ret=calc();
for(rt i=1;i<=n;i++)c[i]='0'+'1'-c[i],s[i]='0'+'1'-s[i];
ret+=calc();cout<<ret;
}
return 0;
}

Permutation and Minimum

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define p 1000000007
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans;
int a[605];bool vis[605],nt[605];
int q[605],t;
int f[605][305][305];
int main(){
for(rt i=1;i<=2*n;i++){
if(i&1^1){
if(a[i]==-1&&a[i-1]==-1)cnt++;
if(a[i]!=-1&&a[i-1]!=-1)nt[a[i]]=nt[a[i-1]]=1;
}
}
for(rt i=1;i<=2*n;i++)if(!nt[i])if(vis[i])q[++t]=1;else q[++t]=0;n=t;
//q=1在原数列中
f[n][0][0]=1;
for(rt i=n;i>=1;i--){
for(rt j=0;j<=n/2;j++)
for(rt k=0;k<=n/2;k++)if(f[i][j][k]){
const int v=f[i][j][k];
if(q[i]){
(f[i-1][j+1][k]+=v)%=p;
if(k)(f[i-1][j][k-1]+=v)%=p;
}else {
(f[i-1][j][k+1]+=v)%=p;
if(k)(f[i-1][j][k-1]+=v)%=p;
if(j)(f[i-1][j-1][k]+=1ll*v*j%p)%=p;
}
}
}
int ans=f[0][0][0];
for(rt i=2;i<=cnt;i++)ans=1ll*ans*i%p;
cout<<ans;
return 0;
}

posted @ 2019-03-05 19:15  Kananix  阅读(175)  评论(0编辑  收藏