首页 > 代码库 > APIO 2014

APIO 2014

练习赛,评测的时候好像出了些问题,最后我拿自己机子测的212/300,第二题负责评测的写的SPJ就判了第一行的答案,不知道有没出什么问题。

先贴代码

 

T1.palindrome 92/100

技术分享
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
#define MN 300000
#define MV 1200000
#define MOD 9875321
char s[MN+5],st[MN*2+5];
int l[MN*2+5],f[MN*2+5],cnt,rr[MV+5],ss[MV+5];
ll hs[MV+5];
struct P{int p,l;}pi[MV+5];
bool cmp(P a,P b){return a.l<b.l;}
struct map
{
    int h[MOD],nx[MV*3/2+5],z[MV*3/2+5],en,lz;
    ll s[MV*3/2+5],ls;
    int&operator[](ll x)
    {
        if(x==ls)return z[lz];
        int p=(x%MOD+MOD)%MOD,i;
        for(i=h[p];i;i=nx[i])if(s[i]==x)return z[lz=i];
        nx[!en||z[en]?++en:en]=h[p];s[en]=x;h[p]=en;return z[lz=en];
    }
}mp;
int main()
{
    freopen("palindrome.in","r",stdin);
    freopen("palindrome.out","w",stdout);
    int i,r=0,p;ll mx=0;
    scanf("%s",s);
    for(st[i=0]=(;s[i];++i)st[(i<<1)+1]=.,st[i+1<<1]=s[i];st[(i<<1)+1]=.;st[i+1<<1]=0;
    for(i=1;st[i];++i)
    {
        if(r>i)l[i]=min(l[(p<<1)-i],r-i),f[i]=(p<<1)-i;
        else l[i]=1;
        pi[++cnt]=(P){i,l[i]};
        while(st[i-l[i]]==st[i+l[i]])pi[++cnt]=(P){i,++l[i]};
        if(i+l[i]>r)r=i+l[i],p=i;
    }
    sort(pi+1,pi+cnt+1,cmp);
    for(i=1;i<=cnt;++i)
    {
        if(pi[i].l>1)
        {
            for(r=pi[i].p;!mp[(ll)r*(MV+3)+pi[i].l-1];r=f[r]);
            hs[i]=hs[rr[i]=mp[(ll)r*(MV+3)+pi[i].l-1]];
        }
        hs[i]=hs[i]*31+st[pi[i].p+pi[i].l-1];
        mp[(ll)pi[i].p*(MV+3)+pi[i].l]=i;
    }
    memset(&mp,0,sizeof(mp));
    for(i=cnt;i;--i)
    {
        if(pi[i].l==l[pi[i].p])++ss[i];
        mx=max(mx,(ll)(pi[i].l-1)*(mp[hs[i]]+=ss[i]));
        ss[rr[i]]+=ss[i];
    }
    cout<<mx;
    fclose(stdin);fclose(stdout);return 0;
}
View Code

 

T2.sequence 100/100

#include<cstdio>
#include<iostream>
using namespace std;
#define ll long long
inline int read()
{
    int x=0;char c;
    while((c=getchar())<0||c>9);
    for(;c>=0&&c<=9;c=getchar())x=x*10+c-0;
    return x;
}
#define MK 200
#define MN 100000
int s[MN+5],d[MK+1][MN+5],q[MN+5],l,r;
ll f[2][MN+5];
double cal(int p,int i,int j){return double(f[p][i]-f[p][j])/(s[j]-s[i]-1e-9);}
int main()
{
    freopen("sequence.in","r",stdin);
    freopen("sequence.out","w",stdout);
    int n,k,i,j,p,pl;
    n=read();k=read();
    for(i=1;i<=n;++i)s[i]=s[i-1]+read();
    for(i=p=1,pl=0;i<=k;++i,p^=1,pl^=1)
    {
        l=r=0;
        for(j=1;j<=n;++j)
        {
            while(l<r&&cal(pl,q[l+1],q[l])<s[j])++l;
            d[i][j]=q[l];
            f[p][j]=f[pl][q[l]]+(ll)s[j]*s[q[l]];
            f[pl][j]-=(ll)s[j]*s[j];
            while(l<r&&cal(pl,j,q[r])<cal(pl,q[r],q[r-1]))--r;
            q[++r]=j;
        }
    }
    cout<<f[pl][n]<<endl;
    for(i=n,j=k;j;--j)printf("%d ",i=d[j][i]);
    fclose(stdin);fclose(stdout);return 0;
}

 

T3.beads 20/100

技术分享
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read()
{
    int x=0;char c;
    while((c=getchar())<0||c>9);
    for(;c>=0&&c<=9;c=getchar())x=x*10+c-0;
    return x;
}
#define MN 200000
#define INF 0x3fffffff
struct edge{int nx,t,w;}e[MN*2+5];
int h[MN+5],en,f[MN+5][2];
inline void ins(int x,int y,int w)
{
    e[++en]=(edge){h[x],y,w};h[x]=en;
    e[++en]=(edge){h[y],x,w};h[y]=en;
}
void dp(int x,int fa)
{
    int i,p,mx=-INF,mx2=-INF;
    for(i=h[x];i;i=e[i].nx)if(e[i].t!=fa)
    {
        dp(e[i].t,x);
        f[x][0]+=p=max(f[e[i].t][1]+e[i].w,f[e[i].t][0]);
        p=f[e[i].t][0]+e[i].w-p;
        if(p>mx)mx2=mx,mx=p;
        else if(p>mx2)mx2=p;
    }
    f[x][1]=f[x][0]+mx;
    f[x][0]=max(f[x][0],f[x][0]+mx+mx2);
}
int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
    int n=read(),i,x,y;
    for(i=1;i<n;++i)x=read(),y=read(),ins(x,y,read());
    dp(1,0);
    printf("%d",f[1][0]);
    fclose(stdin);fclose(stdout);return 0;
}
View Code

 

APIO 2014