NOIP2018游记

· · 个人记录

作为一个FZSZ的高二OIer,或许考完今年的NOIP就退役qwq

Day1 T1

首尾相接

环状积木大赛???

题目有毒...

手算样例,答案是7...

就是NOIP2013积木大赛原题(雾

但是,我好像学傻了,写了个单调栈+排序+树状数组 反正能$AC

预计得分100

#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cstring>
#define _rep(i,s,t) for(register int i=s;i>=t;--i)
#define rep(i,s,t) for(register int i=s;i<=t;++i)
using namespace std;
typedef long long ll;
const int N=4e5+11;
int n,m,mn,top;
int a[N],st[N],l[N],r[N],id[N],s[N];
ll res;
inline bool cmp(int x,int y){
    return a[x]<a[y];
}
inline void ins(int x,int y){
    for(;x<=n;x+=x&-x)
        s[x]+=y;
}
inline int query(int x){
    int res=0;
    for(;x;x-=x&-x)
        res+=s[x];
    return res;
}
int main(){
    freopen("road.in","r",stdin);
    freopen("road.out","w",stdout);
    scanf("%d",&n);
    rep(i,1,n)
        scanf("%d",a+i);
    rep(i,1,n){
        while(st[0]&&a[st[st[0]]]>a[i])
            r[st[st[0]]]=i-1,--st[0];
        st[++st[0]]=i;
    }
    rep(i,1,st[0])
        r[st[i]]=n;
    st[0]=0;
    _rep(i,n,1){
        while(st[0]&&a[st[st[0]]]>a[i])
            l[st[st[0]]]=i+1,--st[0];
        st[++st[0]]=i;
    }
    rep(i,1,st[0])
        l[st[i]]=1;
    rep(i,1,n)
        id[i]=i;
    sort(id+1,id+n+1,cmp);
    rep(i,1,n){
        int u=id[i];
        int ans=query(u);
        if(a[u]==ans)continue;
        res+=a[u]-ans;
        ins(l[u],a[u]-ans);
        ins(r[u]+1,ans-a[u]);
    }
    cout<<res<<endl;
    return 0;
}
/*
6
4 3 2 5 3 5
*/
T2

小凯的疑惑加强版???

傻逼背包,10min写完

听说是道原题BZOJ2914

预计得分100

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cstring>
#define rep(i,s,t) for(register int i=s;i<=t;++i)
using namespace std;
const int N=50011;
bool f[N];
int T,n,a[N],ans,mx;
int main(){
    freopen("money.in","r",stdin);
    freopen("money.out","w",stdout);
    scanf("%d",&T);
    f[0]=1;
    for(;T--;){
        mx=0;
        scanf("%d",&n);
        rep(i,1,n)
            scanf("%d",a+i),
            mx=max(mx,a[i]);
        sort(a+1,a+n+1);
        ans=n;
        rep(i,1,n){
            if(f[a[i]])--ans;
            else{
                rep(j,a[i],mx)
                    f[j]|=f[j-a[i]];
            }
        }
        rep(i,1,mx)
            f[i]=0;
        printf("%d\n",ans);
    }
    return 0;
}
T3

大家都写了二分套二分,就我写了个二分套set qwq

听说又是半道原题$BZOJ2067

预计得分100

#include<cstdio>
#include<algorithm>
#include<set>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#define _rep(i,s,t) for(register int i=s;i>=t;--i)
#define rep(i,s,t) for(register int i=s;i<=t;++i)
using namespace std;
const int N=4e5+11;
struct node{
    int id,val;
    node(int Id=0,int Val=0){
        id=Id,val=Val;
    }
    inline bool operator<(node A)const{
        if(val==A.val)return id<A.id;
        return val<A.val;
    }
};
set<node>s;
set<node>::iterator it;
int n,m,x,y,z,tot,ans,dfc;
int nxt[N],las[N],to[N],w[N],xu[N],fa[N],f[N];
bool vis[N];
int a[N];
inline void add(int x,int y,int z){
    nxt[++tot]=las[x];
    las[x]=tot;
    to[tot]=y;
    w[tot]=z;
}
inline void dfs(int x,int fa){
    xu[++dfc]=x;
    for(register int e=las[x];e;e=nxt[e]){
        if(to[e]==fa)continue;
        ::fa[to[e]]=x;
        dfs(to[e],x);
    }
}
inline bool check(int mid){
    int cnt=0,x,top,l,r;
    rep(i,1,n)
        f[i]=0;
    _rep(i,n,1){
        x=xu[i];
        top=0;
        for(register int e=las[x];e;e=nxt[e]){
            if(to[e]==fa[x])continue;
            if(f[to[e]]+w[e]>=mid){
                ++cnt;
                if(cnt>=m)
                    return 1;
            }
            else
                a[++top]=f[to[e]]+w[e];
        }
                s.clear();
                sort(a+1,a+top+1);
                rep(j,1,top){
                    vis[j]=0;
                    s.insert(node(j,a[j]));
                }
                //printf("%d\n",x);
                //rep(j,1,top)printf("%d ",a[j]);
                //puts("");
                rep(j,1,top){
                    if(vis[j])continue;
                    it=s.lower_bound(node(j,mid-a[j]));
                    if(it==s.end())continue;
                if((*it).id!=j){
                    vis[(*it).id]=vis[j]=1;
                    ++cnt;
                    if(cnt>=m)
                        return 1;
                    s.erase(node(j,a[j]));
                    s.erase(it);
                }
            }

        //printf("cnt=%d\n",cnt);
        _rep(j,top,1)
            if(!vis[j]){
                f[x]=a[j];
                break;
            }

    }
    //printf("%d\n",cnt);
    return cnt>=m;
}
int tt,maxlen;
inline void Dfs(int x,int fa,int s=0){
    if(s>maxlen)
        maxlen=s,tt=x;
    for(register int e=las[x];e;e=nxt[e])
        if(fa!=to[e])
            Dfs(to[e],x,s+w[e]);
}   
int main(){
    freopen("track.in","r",stdin);
    freopen("track.out","w",stdout);
    scanf("%d%d",&n,&m);
    rep(i,1,n-1){
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z),add(y,x,z);
    }
    if(m==1){
        Dfs(1,0);
        maxlen=0;
        Dfs(tt,0);
        printf("%d\n",maxlen);
        return 0;
    }
    dfs(1,0);
    _rep(i,30,0)
        if(check(ans|(1<<i)))
            ans|=(1<<i);
    //++ans;
    printf("%d\n",ans);
    return 0;
}
/*
9 3
1 2 6
2 3 3
3 4 5
4 5 10
6 2 4
7 2 9
8 4 7
9 4 4

7 1
1 2 10
1 3 5
2 4 9
2 5 8
3 6 6
3 7 7
*/
Day1$ $AK$大赛,$2.5$道原题,大家都$300

预计得分 100+100+100=300

Day2 T1

基环树上找环,枚举删边,暴力dfs即可

预计得分100

#include<cstdio>
#include<algorithm>
#define rep(i,s,t) for(register int i=s;i<=t;++i)
using namespace std;
const int N=5011,M=2e4+11;
int n,m,tot=1,tim,x[N],y[N],xu[N],dfc,low[N],dfn[N],st[N],c[N];
int to[N][N],deg[N],top,cnt,flag;
int nxt[M],las[M],To[M],ans[N];
bool vis[N],X,Y;
inline void dfs(int x,int fa){
    xu[++dfc]=x;
    rep(i,1,deg[x])
        if(to[x][i]!=fa&&to[x][i])
            dfs(to[x][i],x);
}
inline void Dfs(int x,int fa){
    low[x]=dfn[x]=++tim;
    st[++top]=x;
    vis[x]=1;
    rep(i,1,deg[x]){
        if(to[x][i]==fa)continue;
        if(!dfn[to[x][i]]){
            Dfs(to[x][i],x);
            low[x]=min(low[x],low[to[x][i]]);
        }
        else
            if(vis[to[x][i]])
                low[x]=min(low[x],dfn[to[x][i]]);
    }
    if(low[x]==dfn[x]){
        int u;
        while(u=st[top--],u!=x)
            c[++cnt]=u,vis[u]=0;
        if(cnt&&!flag)
            c[++cnt]=x,vis[x]=0,flag=1;
    }
}
inline void del(int x,int y){
    for(register int i=1;i<=deg[x];++i)
        if(to[x][i]==y){
            to[x][i]=0;
            break;
        }
}
inline void ins(int x,int y){
    for(register int i=1;i<=deg[x];++i)
        if(!to[x][i]){
            to[x][i]=y;
            break;
        }
}
inline void check(){
    rep(i,1,n)
        if(xu[i]!=ans[i]){
            if(ans[i]<xu[i])return;
            else break;
        }
    rep(i,1,n)
        ans[i]=xu[i];
}
int main(){
    freopen("travel.in","r",stdin);
    freopen("travel.out","w",stdout);
    scanf("%d%d",&n,&m);
    rep(i,1,m){
        scanf("%d%d",x+i,y+i);
        ++deg[x[i]],++deg[y[i]];
        to[x[i]][deg[x[i]]]=y[i],to[y[i]][deg[y[i]]]=x[i];
    }
    rep(i,1,n)
        sort(to[i]+1,to[i]+deg[i]+1);
    if(m==n-1){
        dfs(1,0);
        rep(i,1,n){
            printf("%d",xu[i]);
            if(i==n)puts("");
            else printf(" ");
        }
        return 0;
    }
    Dfs(1,0);
    ans[1]=n;
    rep(i,1,cnt){
        dfc=0;
        del(c[i],c[i%cnt+1]),del(c[i%cnt+1],c[i]);
        dfs(1,0);
        check();
        ins(c[i],c[i%cnt+1]),ins(c[i%cnt+1],c[i]);
    }
    rep(i,1,n){
        printf("%d",ans[i]);
        if(i==n)puts("");
        else printf(" ");
    }
    return 0;
}
/*
6 5
1 3
2 3
2 5
3 4
4 6

6 6
1 3
2 3
2 5
3 4
4 5
4 6
*/
T2

难到爆炸的数数题,写dfs,找规律

假设n<=m

n=1,ans=2^m n=2,ans=12*3^{m-2} n=3,ans=112*3^{m-3} 预计得分$75
#include<cstdio>
#include<algorithm>
#define rep(i,s,t) for(register int i=s;i<=t;++i)
using namespace std;
const int mod=1e9+7,N=1e6+11;
int n,m,ans,flag,top;
int a[9][N],xu[N],res[N];
inline void copy(){
    rep(i,1,n+m-1)
        res[i]=xu[i];
}
inline void Dfs(int x,int y){
    if(flag)return;
    if(x==n&&y==m){
        rep(i,1,n+m-1){
            if(res[i]>xu[i]){
                flag=1;
                return;
            }
            if(res[i]<xu[i]){
                copy();
                return;
            }
        }
        return ;
    }
    xu[x+y-1]=a[x][y];
    if(y==m){
        Dfs(x+1,y);
        return ;
    }
    if(x==n){
        Dfs(x,y+1);
        return;
    }
    Dfs(x,y+1);
    Dfs(x+1,y);
}
inline void dfs(int x,int y){
    if(x==n+1){
        flag=0;
        rep(i,1,n+m-1)
            res[i]=xu[i]=0;
        Dfs(1,1);
        if(!flag){
            ++ans;
            /*rep(i,1,n){
                rep(j,1,m)
                    printf("%d ",a[i][j]);
                puts("");
            }*/
            //puts("");
        }
        return ;
    }
    if(y==m+1){
        dfs(x+1,1);
        return ;
    }
    if(!a[x-1][y+1])
        a[x][y]=0,dfs(x,y+1);
    a[x][y]=1;
    dfs(x,y+1);
}
inline int fp(int a,int b){
    int res=1;
    for(;b;b>>=1,a=1ll*a*a%mod)
        if(b&1)
            res=1ll*res*a%mod;
    return res;
}
int main(){
    freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);
    //n=2,m=2;
    scanf("%d%d",&n,&m);
    if(n>m)swap(n,m);
    if(n==1){
        printf("%d\n",fp(2,m));
        return 0;
    }
    if(n==2){
        int ans=12ll*fp(3,m-n)%mod;
        printf("%d\n",ans);
        return 0;
    }
    if(n==3){
        int ans=112ll*fp(3,m-n)%mod;
        printf("%d\n",ans);
        return 0;
    }
    if(n==4){
        /*
        4 4 912
        4 5 2688
        4 6 8064
        4 7 24912
        4 8 72576
        */
        if(m==4)puts("912");
        if(m==5)puts("2688");
        if(m==6)puts("8064");
        if(m==7)puts("24912");
        if(m==8)puts("72576");
        return 0;
    }
    if(n==5){
        /*
        5 5 7136
        5 6 21312
        5 7 63936
        5 8 191808
        */
        if(m==5)puts("7136");
        if(m==6)puts("21312");
        if(m==7)puts("63936");
        if(m==8)puts("191808");
        return 0;
    }
    if(n==6){
        /*
        6 6 56768
        6 7 170112
        6 8 510336
        */
        if(m==6)puts("56768");
        if(m==7)puts("170112");
        if(m==8)puts("510336");
        return 0;
    }
    if(n==7){

    }
    if(n==8){

    }
    dfs(1,1);
    printf("%d\n",ans);
    return 0;
}
/*
1 1
2 12
3 112
4 912
5 5 7136
6 6 56768


4 4 912
4 5 2688
4 6 8064
4 7 24912
4 8 72576
5 5 7136
5 6 21312
5 7 63936
5 8 191808
6 6 56768
6 7 170112
6 8 510336

*/
T3

一眼ddp,告辞,写了44暴力dp

预计得分44

#include<cstdio>
#include<algorithm>
#include<cstdlib>
#define rep(i,s,t) for(register int i=s;i<=t;++i)
using namespace std;
typedef long long ll;
const int N=6e5+11;
const ll inf=(1ll<<60);
ll ans;
int n,m,x,y,tot,a,b;
int nxt[N],las[N],to[N],p[N],fa[N];
char s[6];
inline void add(int x,int y){
    nxt[++tot]=las[x];
    las[x]=tot;
    to[tot]=y;
}
ll f[N][2];
inline void dfs(int x,int fa){
    if(f[x][0]<inf)f[x][0]=0;
    if(f[x][1]<inf)f[x][1]=p[x];
    for(register int e=las[x];e;e=nxt[e]){
        if(to[e]==fa)continue;
        dfs(to[e],x);
        if(f[x][1]<inf)
            f[x][1]+=min(f[to[e]][0],f[to[e]][1]);
        if(f[x][0]<inf)
            f[x][0]+=f[to[e]][1];
    }
}
inline void Dfs(int x,int anc){
    fa[x]=anc;
    for(register int e=las[x];e;e=nxt[e])
        if(to[e]!=anc){
            Dfs(to[e],x);
        }
}
ll sum[N][2];
inline void up(int b){
    while(fa[b]){
        b=fa[b];
        f[b][0]=f[b][1]=0;
        for(register int e=las[b];e;e=nxt[e])
            if(to[e]!=fa[b]){
                f[b][1]+=min(f[to[e]][1],f[to[e]][0]);
                f[b][0]+=f[to[e]][1];
            }
        f[b][1]+=p[b];
    }
}
int main(){
    freopen("defense.in","r",stdin);
    freopen("defense.out","w",stdout);
    scanf("%d%d",&n,&m);
    scanf("%s",s+1);
    rep(i,1,n)
        scanf("%d",p+i);
    rep(i,2,n){
        scanf("%d%d",&x,&y);
        add(x,y),add(y,x);
    }
    if(n<=2000){
        while(m--){
            scanf("%d%d%d%d",&a,&x,&b,&y);
            rep(i,1,n)
                f[i][0]=f[i][1]=0;
            f[a][x^1]=inf,f[b][y^1]=inf;
            dfs(1,0);
            ans=min(f[1][0],f[1][1]);
            if(ans>=inf)puts("-1");
            else printf("%lld\n",ans);
            //rep(i,1,n)rep(j,0,1)printf("f[%d][%d]=%lld\n",i,j,f[i][j]);
            f[a][x^1]=0,f[b][y^1]=0;
        }
        return 0;
    }
    Dfs(1,0);
    dfs(1,0);
    while(m--){
        scanf("%d%d%d%d",&a,&x,&b,&y);
        f[b][y^1]=inf,up(b);
        f[a][x^1]=inf,up(a);
        dfs(1,0);
        ans=min(f[1][0],f[1][1]);
        if(ans>=inf)puts("-1");
        else printf("%lld\n",ans);
            //rep(i,1,n)rep(j,0,1)printf("f[%d][%d]=%lld\n",i,j,f[i][j]);
        f[a][x^1]=0,up(b);
        f[b][y^1]=0,up(a);
    }
    return 0;
}
/*
5 3 C3
2 4 1 3 9
1 5
5 2
5 3
3 4
1 0 3 0
2 1 3 1
1 0 5 0
*/
预计得分$100+75+44=219

总分 100+100+100+100+75+44=519

今年的NOIP打扰了,告辞,我先退役为敬

GGF测出来 100+100+95+100+70+44=509

我果然还是太弱了