Codeforces Round #564 (Div. 1)

A

Too hard, just half of the time this problem has not to do it, just from your fences. In fact the two cases, a very simple and direct put, and the other is to 0,0, ..., 0,1,2, ..., n, and then directly greedy, obviously I was wrong to judge the situation has not tune out .

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+7;
int n,p,ans,a[N],b[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=1;i<=n;i++)if(b[i]==1)p=i;
    if(p)
    {
        bool flag=1;
        for(int i=p+1;i<=n;i++)if(b[i-1]+1!=b[i]){flag=0;break;}
        if(flag)
        {
            for(int i=1;i<p;i++)if(b[i]&&b[i]<n+2+i-p){flag=0;break;}
            if(flag){printf("%d",p-1);return 0;}
        }
    }
    for(int i=1;i<=n;i++)if(b[i])ans=max(ans,i-b[i]+1);
    printf("%d",n+ans);
}
View Code

B

Sign.

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,mod=998244353;
int n,f[N],fac[N];
vector<int>G[N];
void dfs(int u,int fa)
{
    f[u]=fac[G[u].size()];
    for(int i=0;i<G[u].size();i++)if(G[u][i]!=fa)dfs(G[u][i],u),f[u]=1ll*f[u]*f[G[u][i]]%mod;
}
int main()
{
    scanf("%d",&n);
    for(int i=1,x,y;i<n;i++)scanf("%d%d",&x,&y),G[x].push_back(y),G[y].push_back(x);
    fac[0]=1;for(int i=1;i<=n;i++)fac[i]=1ll*fac[i-1]*i%mod;
    dfs(1,0);
    f[1]=1ll*f[1]*n%mod;
    printf("%d",f[1]);
}
View Code

C

C1 is very simple, it is easy to think of a O (nm . 3 ) approach is engaged collapse A mentality then wrote a year, is the direct vigorously DP, f [T] [i ] [j] [k] denotes the operation times T after the i-th item of the total weight of the weight change j k is the probability, can be pushed directly remember rolling array.

C2 is actually to find a conclusion that the probability of this conclusion, I did not think no natural write. a [i] the same article, the final weight of expectation into the original scale, this is probably my high school did not learn mathematics. f [i] [j] represents the probability of plus minus i times j times, but also vigorously transferred to the complexity of O (m 2 logm + nlogn) is likely to be card often, how do? Pretreatment may be optimized to inverse element O (m 2 + n-) or O (m 2 + nlogn), only one pretreatment adjacent to 2m.

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,M=3003,mod=998244353;
int n,m,sum,s[2],p[2],a[N],w[N],f[M][M],inv[M*2];
int qpow(int a,int b)
{
    int ret=1;
    while(b)
    {
        if(b&1)ret=1ll*ret*a%mod;
        a=1ll*a*a%mod,b>>=1;
    }
    return ret;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&w[i]),sum+=w[i],s[a[i]]+=w[i];
    for(int i=0;i<=2*m;i++)if(sum+i-m>=0)inv[i]=qpow(sum+i-m,mod-2);
    f[0][0]=1;
    for(int i=0;i<m;i++)
    for(int j=0;i+j<m;j++)
    if(f[i][j])
    {
        int add=1ll*(s[1]+i)*inv[m+i-j]%mod,dec=1ll*(s[0]-j)*inv[m+i-j]%mod;
        f[i+1][j]=(f[i+1][j]+1ll*f[i][j]*add)%mod;
        f[i][j+1]=(f[i][j+1]+1ll*f[i][j]*dec)%mod;
    }
    for(int i=0;i<=m;i++)if(f[i][m-i])
    p[1]=(p[1]+1ll*(s[1]+i)*f[i][m-i])%mod,p[0]=(p[0]+1ll*(s[0]-m+i)*f[i][m-i])%mod;
    for(int i=1;i<=n;i++)
    printf("%d\n",1ll*w[i]*qpow(s[a[i]],mod-2)%mod*p[a[i]]%mod);
}
View Code

D

Consider the n * n grid becomes (n-1) * (n -1) of the grid, if the conditions are met, do not judge, or to find the last line should be placed in the last one and the last one which should be placed in The last line of which, the two positions and put on a portal can be. In fact, you can do O (n), but the small data range, and I'm violence O (the n- 2 ) to find.

#include<bits/stdc++.h>
using namespace std;
const int N=1019;
int n,m,a[N],b[N],a1[N],b1[N],a2[N],b2[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=n;i;i--)
    if(a[i]!=i||b[i]!=i)
    {
        for(int j=1;j<=i;j++)if(a[j]==i)a[j]=a[i];
        for(int j=1;j<=i;j++)if(b[j]==i)b[j]=b[i];
        a1[++m]=a[i],b1[m]=i,a2[m]=i,b2[m]=b[i];
    }
    printf("%d\n",m);
    for(int i=1;i<=m;i++)printf("%d %d %d %d\n",a1[i],b1[i],a2[i],b2[i]);
}
View Code

EF

Gugu Gu.

result: trumpet playing. rank203, rating + = 5, fortunately tuba otherwise useless lost rating steady.

Guess you like

Origin www.cnblogs.com/hfctf0210/p/10990128.html