Contest 8

  A:做法应该很多,比较好想的是每个点都往上倍增找到其能更新到的点。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
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;
}
#define N 200010
int n,fa[N][19],ans[2][N];
struct data{int x,ch[2];
}tree[N];
int whichson(int k){return tree[fa[k][0]].ch[1]==k;}
int main()
{
    freopen("node.in","r",stdin);
    freopen("node.out","w",stdout);
    n=read();
    for (int i=1;i<=n;i++) tree[i].x=read();
    for (int i=1;i<=n;i++)
    fa[tree[i].ch[0]=read()][0]=i,fa[tree[i].ch[1]=read()][0]=i;
    fa[0][0]=0;
    for (int j=1;j<19;j++)
        for (int i=1;i<=n;i++)
        fa[i][j]=fa[fa[i][j-1]][j-1];
    for (int i=1;i<=n;i++)
    {
        int k=tree[i].x,x=i;
        for (int j=18;~j;j--) if (k>(1<<j)) x=fa[x][j],k-=(1<<j);
        ans[whichson(x)][fa[x][0]]++;
    }
    for (int i=1;i<=n;i++) printf("%d %d\n",ans[0][i],ans[1][i]);
    return 0;
}
View Code

  B:直接模拟即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
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;
}
#define P 1000000007
int n,m,a,b,c,x,y,ansx,ansy;
long long f;
bool flag[50000010];
void inc(int &x,int y){x+=y;if (x>=P) x-=P;}
int main()
{
    freopen("schedule.in","r",stdin);
    freopen("schedule.out","w",stdout);
    n=read(),m=read(),a=read(),b=read(),c=read();
    for (int i=1;i<=m;i++)
    {
        f=(1ll*a*f+b)%(2ll*n*c);
        if (f<1ll*n*c)
        {
            int t=f/c+1;
            if (!flag[t]) 
            {
                flag[t]=1;
                inc(x,1),inc(y,t);
            }
        }
        else
        {
            int t=f/c-n+1;
            if (flag[t])
            {
                flag[t]=0;
                inc(x,P-1),inc(y,P-t);
            }
        }
        inc(ansx,x),inc(ansy,y);
    }
    cout<<ansx<<' '<<ansy<<endl;
    return 0;
}
View Code

  C:考虑扩展最大独立集的做法:设f[i][j]为i子树中离i最近的被选点与i的距离至少为j时的最优解,转移时枚举与根最近的点在哪棵子树及与根的距离即可转移。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
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;
}
#define N 10010
#define M 110
int n,m,a[N],p[N],f[N][M],t=0;
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)
{
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from) dfs(edge[i].to,k);
    for (int j=0;j<=m;j++)
    {
        int tot=0;
        for (int i=p[k];i;i=edge[i].nxt)
        if (edge[i].to!=from) tot+=f[edge[i].to][max(j,m-j-1)];
        if (j==m) f[k][0]=tot+a[k];
        for (int i=p[k];i;i=edge[i].nxt)
        if (edge[i].to!=from) f[k][j+1]=max(f[k][j+1],f[edge[i].to][j]+tot-f[edge[i].to][max(j,m-j-1)]);
    }
    for (int i=m;~i;i--)
    f[k][i]=max(f[k][i+1],f[k][i]);
}
int main()
{
    freopen("score.in","r",stdin);
    freopen("score.out","w",stdout);
    n=read(),m=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);
    }
    dfs(1,1);
    cout<<f[1][0];
    return 0;
}
View Code

  result:300 rank1

posted @ 2018-10-06 02:01  Gloid  阅读(195)  评论(0编辑  收藏  举报