AISing Programming Contest 2019 翻车记
A:签到。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define ll long long int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int h,w,n; int main() { n=read(),h=read(),w=read(); cout<<(n-w+1)*(n-h+1); }
B:签到*2。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 110 int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,a,b,p[N]; int main() { n=read(),a=read(),b=read(); for (int i=1;i<=n;i++) p[i]=read(); int ans=0,x=0,y=0,z=0; for (int i=1;i<=n;i++) { if (p[i]<=a) x++; if (p[i]>a&&p[i]<=b) y++; if (p[i]>b) z++; } cout<<min(x,min(y,z)); }
C:考虑在相邻两异色格间连边,这样同一个连通块的异色点间一定存在合法路径。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 410 int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,a[N][N],fa[N*N],size[N*N][2]; int trans(int x,int y){return (x-1)*m+y;} int wx[4]={1,0,0,-1},wy[4]={0,1,-1,0}; int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} int main() { n=read(),m=read(); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { char c=getchar(); while (c!='.'&&c!='#') c=getchar(); a[i][j]=c=='.'; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { fa[trans(i,j)]=trans(i,j); size[trans(i,j)][a[i][j]]=1; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) for (int k=0;k<4;k++) if (i+wx[k]>=1&&i+wx[k]<=n&&j+wy[k]>=1&&j+wy[k]<=m&&a[i+wx[k]][j+wy[k]]!=a[i][j]) { int p=find(trans(i,j)),q=find(trans(i+wx[k],j+wy[k])); if (p!=q) { fa[p]=q; size[q][0]+=size[p][0],size[q][1]+=size[p][1]; } } ll ans=0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (find(trans(i,j))==trans(i,j)) ans+=1ll*size[trans(i,j)][0]*size[trans(i,j)][1]; cout<<ans; }
D:显然分为两个过程,第一个过程中两人取数互不相关,第二个过程中两人从大到小依次取数。二分套二分找到分界点,瞎搞一波前缀和即可。不知道为啥写了一年。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 100010 int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,a[N]; ll s[N],f[N]; int findmax(int x,int k) { int l=1,r=n; while (l+1<r) { int mid=l+r>>1; if (mid+k-1>n) r=mid; else if (x-a[mid]>a[mid+k-1]-x) l=mid; else r=mid; } int ans=l; for (int i=l+1;i<=r;i++) if (r+k-1<=n&&max(x-a[i],a[i+k-1]-x)<max(x-a[ans],a[ans+k-1]-x)) ans=i; return a[ans+k-1]; }//和x最接近的数中取k个后最大的 bool check(int k,int x) { if (k*2-1>n) return 0; //cout<<k<<' '<<x<<endl; //cout<<findmax(x,1)<<' '<<a[n-k+1]<<endl; return findmax(x,k-1)<a[n-k+1]; } int main() { n=read(),m=read(); for (int i=1;i<=n;i++) a[i]=read(); sort(a+1,a+n+1); s[1]=a[1]; for (int i=2;i<=n;i++) s[i]=s[i-2]+a[i]; for (int i=n;i>=1;i--) f[i]=f[i+1]+a[i]; for (int i=1;i<=m;i++) { int x=read(); int l=1,r=n,t=0; while (l<=r) { int mid=l+r>>1; if (check(mid,x)) t=mid,l=mid+1; else r=mid-1; } //cout<<t<<endl; ll ans=f[n-t+1];int y=n-t*2; if (y>0) ans+=s[y]; printf("%lld\n",ans); } }
E:正常的想法是设f[i][j]为i子树中包含j的连通块权值和为j时最少要割多少边,显然这样不行,于是就反过来,设f[i][j]为i子树中割j条边能使i所在连通块获得的最小权值和(当然割出的除根以外的连通块均需要合法)。同时设g[i]表示i子树中最少割多少条边能使i所在连通块均为正数(前提同上)。转移类似树形背包。莫名其妙写的非常慢感觉非常恶心。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 5010 int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,a[N],p[N],g[N],size[N],t; ll sum[N],f[N][N]; struct data{int to,nxt; }edge[N<<1]; void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;} void dfs(int k,int from) { sum[k]=a[k]; for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) { dfs(edge[i].to,k); sum[k]+=sum[edge[i].to]; } if (a[k]>0) { g[k]=0; for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) { int x=g[edge[i].to]; for (int j=0;j<=n;j++) if (f[edge[i].to][j]<0) {x=min(x,j+1);break;} g[k]+=x; } } f[k][0]=a[k]; size[k]=1; for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) { for (int j=size[k]+size[edge[i].to];j>=0;j--) { f[k][j]+=sum[edge[i].to];if (j-g[edge[i].to]-1>=0) f[k][j]=min(f[k][j],f[k][j-g[edge[i].to]-1]); for (int x=max(0,j-size[k]);x<=min(j,size[edge[i].to]);x++) { if (x) f[k][j]=min(f[k][j],f[k][j-x]+f[edge[i].to][x]); if (x<j&&f[edge[i].to][x]<0) f[k][j]=min(f[k][j],f[k][j-x-1]); } } size[k]+=size[edge[i].to]; } } int main() { /*freopen("e.in","r",stdin); freopen("e.out","w",stdout);*/ n=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<n;i++) { int x=read(),y=read(); addedge(x,y),addedge(y,x); } memset(g,42,sizeof(g)); memset(f,42,sizeof(f)); dfs(1,1); /*for (int i=1;i<=n;i++) cout<<a[i]<<' ';cout<<endl; for (int i=1;i<=n;i++) cout<<g[i]<<' ';cout<<endl; cout<<endl; for (int i=1;i<=n;i++) { for (int j=0;j<n;j++) cout<<f[i][j]<<' '; cout<<endl;; } for (int i=1;i<=n;i++) cout<<sum[i]<<' ';cout<<endl;*/ int ans=g[1]; for (int i=0;i<=n;i++) if (f[1][i]<0) {ans=min(ans,i);break;} cout<<ans; return 0; }
为什么算rating的时候不把unrated的去掉啊。result:rank 43 rating +138