A:显然应该让未确定的大小尽量大。不知道写了啥就wa了一发。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 200010 #define inf 1000000010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} 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,p[N],fa[N],a[N],s[N],t,ans; bool flag=1; struct data{int to,nxt; }edge[N]; void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;} void dfs(int k) {if (!flag) return;for (int i=p[k];i;i=edge[i].nxt){int x=edge[i].to,son=0;for (int j=p[x];j;j=edge[j].nxt) son++;if (son==0) a[x]=0;else{int y=inf;for (int j=p[x];j;j=edge[j].nxt)y=min(y,s[edge[j].to]);if (y<s[k]) {flag=0;break;}a[x]=y-s[k];s[x]=s[k]+a[x];for (int j=p[x];j;j=edge[j].nxt)a[edge[j].to]=s[edge[j].to]-s[x],dfs(edge[j].to);}} } signed main() { #ifndef ONLINE_JUDGEfreopen("a.in","r",stdin);freopen("a.out","w",stdout);const char LL[]="%I64d\n"; #endifn=read();for (int i=2;i<=n;i++){int x=read();fa[i]=x;addedge(x,i);}for (int i=1;i<=n;i++) s[i]=read();a[1]=s[1];dfs(1);if (flag){ll ans=0;for (int i=1;i<=n;i++) ans+=a[i];cout<<ans;}else cout<<-1;return 0;//NOTICE LONG LONG!!!!! }
B:快1.5h时才想到一个不太靠谱的做法,然后交一发就wa on 4了。拍了好一会才发现做法假掉了,内心崩溃。花40min换了一个做法,写的时候就感觉这怎么这么简单肯定是假的,然后就wa on 7了,根本都不用拍就发现做法假掉了,内心崩溃。最后终于发现最开始的做法是能抢救一下的,把第二种做法合并上去就行了,然后就没时间了。
考虑枚举左上角的2*2方格怎么填。然后考虑前两列,如果将左上角直接向下复制,后面的每一列都只有不相关的两种填法,即两字母交替出现,取最优值即可;如果对左上角复制后进行一些同行内的交换,那么显然会有同一列的连续三行出现不同的字母,这样就固定了每一行都只能是将该行前两个字母复制。
事实上更优美的想法是最终填法要么每行内两字母交替出现,要么每列内两字母交替出现。根本想不到啊。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> #include<vector> using namespace std; #define ll long long #define N 300010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} 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,tot,v=N; vector<char> a[N],b[N],ans[N]; char c[4]={'A','C','G','T'}; bool flag[4]; void check() {//cout<<b[0][0]<<b[0][1]<<endl<<b[1][0]<<b[1][1]<<endl;tot=0;for (int i=2;i<n;i++)b[i][0]=b[i-2][0],b[i][1]=b[i-2][1];for (int j=2;j<m;j++){int ans1=0,ans2=0;for (int i=0;i<n;i++)ans1+=(a[i][j]!=b[i%2][j%2]),ans2+=(a[i][j]!=b[i%2^1][j&1]);if (ans1<ans2){for (int i=0;i<n;i++)b[i][j]=b[i%2][j&1];}else{for (int i=0;i<n;i++)b[i][j]=b[i%2^1][j&1];}}for (int i=0;i<n;i++)for (int j=0;j<m;j++)tot+=a[i][j]!=b[i][j];if (tot<v) {v=tot;for (int i=0;i<n;i++)for (int j=0;j<m;j++)ans[i][j]=b[i][j];}for (int i=2;i<m;i++) b[0][i]=b[0][i&1],b[1][i]=b[1][i&1];for (int i=2;i<n;i++){int ans1=0,ans2=0;for (int j=0;j<m;j++)ans1+=a[i][j]!=b[i%2][j&1],ans2+=a[i][j]!=b[i%2][j&1^1];if (ans1<ans2){for (int j=0;j<m;j++) b[i][j]=b[i%2][j&1];}else{for (int j=0;j<m;j++) b[i][j]=b[i%2][j&1^1];}}tot=0;for (int i=0;i<n;i++)for (int j=0;j<m;j++)tot+=a[i][j]!=b[i][j];if (tot<v) {v=tot;for (int i=0;i<n;i++)for (int j=0;j<m;j++)ans[i][j]=b[i][j];} } void dfs(int x,int y) {if (x>1) {check();return;}for (int i=0;i<4;i++)if (!flag[i]){b[x][y]=c[i];flag[i]=1;if (y==1) dfs(x+1,0);else dfs(x,1);flag[i]=0;} } signed main() { #ifndef ONLINE_JUDGEfreopen("a.in","r",stdin);freopen("a.out","w",stdout);const char LL[]="%I64d\n"; #endifn=read(),m=read();for (int i=0;i<n;i++)for (int j=0;j<m;j++)a[i].push_back(getc());for (int i=0;i<n;i++)for (int j=0;j<m;j++)b[i].push_back(' '),ans[i].push_back(' ');dfs(0,0);for (int i=0;i<n;i++){for (int j=0;j<m;j++)putchar(ans[i][j]);printf("\n");}return 0;//NOTICE LONG LONG!!!!! }
然后就垫底了。感觉应该早点弃掉这个毒瘤B去看C。哎还是菜爆。
大号终于变小号了。result:rank 341 rating -39
C:显然所有点的子树大小之和=所有点的深度之和,那么设度数限制为d,只要找到一组xi满足∑ixi=s且xi>=dxi+1即可。显然度数限制d越大,能构造出s的下界就越小,且增加的这部分边界应该是一段连续区间(因为只要度数限制不为1,就可以找到深度最大且不止一个点的一层,将其中一个点往下拉,使得总深度+1)。于是先找到最小的度数限制,这个随便都能求。然后考虑构造xi,先构造出这个s的下界,再逐渐将其增大。只需要暴力模拟上述证明的过程即可。当然,不能一次只增大1,而是应该直接拉到最底端,以保证复杂度。(虽然事实上只有度数限制<=2时每次增大1的复杂度会出现问题)感觉还是做的太麻烦了。
#include<bits/stdc++.h> using namespace std; #define P 1000000007 #define N 100010 #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 n,ans,fa[N],a[N],size[N],deep[N];ll m; void dfs(int k,int n,int x) {size[k]=1;a[deep[k]]++;if (1ll*(k-1)*x+2>n) return;for (int i=(k-1)*x+2;i<=min(n,k*x+1);i++){deep[i]=deep[k]+1;dfs(i,n,x);size[k]+=size[i];} } ll check(int n,int k) {deep[1]=1;memset(a,0,sizeof(a));dfs(1,n,k);ll s=0;for (int i=1;i<=n;i++) s+=size[i];return s; } void build(int d) {int s=1;for (int i=2;i<=n;i++){for (int j=s+1;j<=s+a[i];j++)fa[j]=s-a[i-1]+(j-s-1)/d+1;s+=a[i];} } int main() { #ifndef ONLINE_JUDGEfreopen("c.in","r",stdin);freopen("c.out","w",stdout); #endifcin>>n>>m;int l=1,r=n-1;if ((1ll*n*(n+1)>>1)<m) {cout<<"NO";return 0;}if ((1ll*n*(n+1)>>1)==m){cout<<"YES\n"; for (int i=1;i<n;i++) printf("%d ",i);return 0;}while (l<=r){int mid=l+r>>1;if (check(n,mid)<=m) ans=mid,r=mid-1;else l=mid+1;}if (!ans) {cout<<"NO";return 0;}ll s=check(n,ans);int cur=n;while (cur&&a[cur]<=1) cur--;int maxdeep;for (maxdeep=1;maxdeep<=n;maxdeep++) if (a[maxdeep]==0) break;maxdeep--;for (;;){if (m==s) break;while (a[cur]==1) cur--;if (maxdeep+1-cur>=m-s) a[cur]--,a[cur+m-s]++,m=s;else a[++maxdeep]++,a[cur]--,s+=maxdeep-cur;}build(ans);cout<<"YES\n";for (int i=2;i<=n;i++) printf("%d ",fa[i]);return 0; }