Noip2016 Ac代码 by liuchenrui

版权声明:233333333333333333333333333333333333333333 https://blog.csdn.net/lcrtest/article/details/53424615

noip2016AC代码
T1

#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<algorithm>
void splay(int&v){
    char s='x';v=0;int p=0;
    while(s<'0'||s>'9'){s=getchar();if(s=='-')p=1;}
    while(s>='0'&&s<='9')v=v*10+s-'0',s=getchar();
    if(p)v=-v;
}
using namespace std;
const int N=100005;
char s[N][15];
int n,t[N],m;
int main(){
    freopen("toy.in","r",stdin);
    freopen("toy.out","w",stdout);
    splay(n);splay(m);
    for(int i=1;i<=n;i++){
        splay(t[i]);
        scanf("%s",s[i]+1);
    }
    int now=1;
    for(int i=1;i<=m;i++){
        int a,b;
        splay(a),splay(b);
        if(a^t[now]){
            now+=b;
            if(now>n)now-=n;
        }
        else{
            now-=b;
            if(now<1)now+=n;
        }
    }
    printf("%s",s[now]+1);
}

T2

#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void splay(int&v){
    char s='x';v=0;int p=0;
    while(s<'0'||s>'9'){s=getchar();if(s=='-')p=1;}
    while(s>='0'&&s<='9')v=v*10+s-'0',s=getchar();
    if(p)v=-v;
}
const int N=300005;
int nxt[N*2],fir[N],to[N*2],sz,deep[N];
int fa[N][22],n,m,ans[N],a[N],s[N],t[N];
void add(int x,int y){
    nxt[++sz]=fir[x],fir[x]=sz,to[sz]=y;
}
void dfs(int x,int f){
    fa[x][0]=f;deep[x]=deep[f]+1;
    for(int i=1;i<=19;i++){
        fa[x][i]=fa[fa[x][i-1]][i-1];
    }
    for(int u=fir[x];u;u=nxt[u]){
        if(to[u]!=f)dfs(to[u],x);
    }
}
int lca(int x,int y){
    if(deep[x]<deep[y])swap(x,y);
    for(int i=19;i>=0;i--){
        if(deep[fa[x][i]]>=deep[y]){
            x=fa[x][i];
        }
    }
    if(x==y)return x;
    for(int i=19;i>=0;i--){
        if(fa[x][i]!=fa[y][i]){
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    return fa[x][0];
}
struct node{
    int a,is;
};
namespace seg{
    int rt[N],ls[N*30],rs[N*30],tot,val[N*30];
    vector<node>v[N];
    void insert(int l,int r,int&w,int pos,int is){
        if(!w)w=++tot;
        is?val[w]++:val[w]--;
        if(l==r)return;
        int mid=l+r>>1;
        if(pos<=mid)insert(l,mid,ls[w],pos,is);
        else insert(mid+1,r,rs[w],pos,is);
    }
    int query(int l,int r,int w,int pos){
        if(l==r)return val[w];
        int mid=l+r>>1;
        if(pos<=mid)return query(l,mid,ls[w],pos);
        else return query(mid+1,r,rs[w],pos);
    }
    void join(int&x,int y){
        if(!x)return x=y,void();
        val[x]+=val[y];
        if(ls[y])join(ls[x],ls[y]);
        if(rs[y])join(rs[x],rs[y]);
    }
    void dfs(int x,int fa){
        for(int u=fir[x];u;u=nxt[u]){
            if(to[u]!=fa){
                dfs(to[u],x);
                if(val[rt[to[u]]]>val[rt[x]])swap(rt[to[u]],rt[x]);
                join(rt[x],rt[to[u]]);
            }
        }
        for(int i=0;i<v[x].size();i++){
            if(v[x][i].is)insert(-2*n,2*n,rt[x],v[x][i].a,1);
            else insert(-2*n,2*n,rt[x],v[x][i].a,0);
        }
        ans[x]+=query(-2*n,2*n,rt[x],a[x]+deep[x]-1);
    }
    void clear(){
        memset(rt,0,sizeof(rt));
        memset(ls,0,sizeof(ls));
        memset(rs,0,sizeof(rs));
        memset(val,0,sizeof(val));
        tot=0;
        for(int i=0;i<=n;i++)v[i].clear();
    }
    void dfs2(int x,int fa){
        for(int u=fir[x];u;u=nxt[u]){
            if(to[u]!=fa){
                dfs2(to[u],x);
                if(val[rt[to[u]]]>val[rt[x]])swap(rt[to[u]],rt[x]);
                join(rt[x],rt[to[u]]);
            }
        }
        for(int i=0;i<v[x].size();i++){
            if(v[x][i].is)insert(-2*n,2*n,rt[x],v[x][i].a,1);
            else insert(-2*n,2*n,rt[x],v[x][i].a,0);
        }
        ans[x]+=query(-2*n,2*n,rt[x],a[x]-deep[x]+1);
    }
}
int main(){
    splay(n),splay(m);
    for(int i=1;i<n;i++){
        int u,v;splay(u),splay(v);
        add(u,v),add(v,u);
    }
    for(int i=1;i<=n;i++){
        splay(a[i]);
    }
    for(int i=1;i<=m;i++){
        splay(s[i]),splay(t[i]);
    }
    dfs(1,0);
    for(int i=1;i<=m;i++){
        int p=lca(s[i],t[i]);
        seg::v[s[i]].push_back((node){deep[s[i]]-1,1});
        seg::v[fa[p][0]].push_back((node){deep[s[i]]-1,0});
    }
    seg::dfs(1,0);
    seg::clear();
    for(int i=1;i<=m;i++){
        int p=lca(s[i],t[i]);
        int x=deep[s[i]]+deep[t[i]]-deep[p]-deep[p];
        x-=deep[t[i]]-1;
        seg::v[t[i]].push_back((node){x,1});
        seg::v[p].push_back((node){x,0});
    }
    seg::dfs2(1,0);
    for(int i=1;i<=n;i++){
        cout<<ans[i];
        if(i!=n)putchar(' ');
    }
}

T3

#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<algorithm>
void splay(int&v){
    char s='x';v=0;int p=0;
    while(s<'0'||s>'9'){s=getchar();if(s=='-')p=1;}
    while(s>='0'&&s<='9')v=v*10+s-'0',s=getchar();
    if(p)v=-v;
}
using namespace std;
const int N=305,M=2005,inf=1000000000;
int dis[N][N],a[M],b[M],s,t,n,m,sum[M];
double p[M],dp[M][M][2];
int main(){
    freopen("classroom.in","r",stdin);
    freopen("classroom.out","w",stdout);
    cin>>s>>t>>n>>m;
    for(int i=1;i<=s;i++)splay(a[i]);
    for(int i=1;i<=s;i++)splay(b[i]);
    for(int i=1;i<=s;i++)scanf("%lf",&p[i]);
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j)dis[i][j]=inf;
    for(int i=1;i<=m;i++){
        int u,v,w;
        splay(u),splay(v),splay(w);
        dis[u][v]=dis[v][u]=min(dis[v][u],w);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            for(int k=1;k<=n;k++){
                if(dis[j][k]>dis[j][i]+dis[i][k]){
                    dis[j][k]=dis[j][i]+dis[i][k];
                }
            }
        }
    }
    memset(dp,127,sizeof dp);
    dp[0][0][0]=0;
    for(int i=1;i<=s;i++){
        dp[i][0][0]=dp[i-1][0][0]+dis[a[i-1]][a[i]];
        for(int j=1;j<=t;j++){
            {
                double x=dp[i-1][j-1][1]
                    +(1-p[i-1])*(1-p[i])*dis[a[i-1]][a[i]]
                    +p[i-1]*(1-p[i])*dis[b[i-1]][a[i]]
                    +(1-p[i-1])*p[i]*dis[a[i-1]][b[i]]
                    +p[i-1]*p[i]*dis[b[i-1]][b[i]];
                double y=dp[i-1][j-1][0]
                    +(1-p[i])*dis[a[i-1]][a[i]]
                    +p[i]*dis[a[i-1]][b[i]];
                dp[i][j][1]=min(x,y);
            }
            {
                double x=dp[i-1][j][1]
                    +(1-p[i-1])*dis[a[i-1]][a[i]]
                    +p[i-1]*dis[b[i-1]][a[i]];
                double y=dp[i-1][j][0]
                    +dis[a[i-1]][a[i]];
                dp[i][j][0]=min(x,y);
            }
        }
    }
    double ans=inf;
    for(int i=0;i<=t;i++){
        ans=min(ans,dp[s][i][0]);
        ans=min(ans,dp[s][i][1]);
    }
    printf("%.2f",ans);
}

T4

#include<cstdio>
#include<iostream>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
void splay(int&v){
    char s='a';v=0;int p=0;
    while(s<'0'||s>'9'){s=getchar();if(s=='-')p=1;}
    while(s>='0'&&s<='9')v=v*10+s-'0',s=getchar();
    if(p)v=-v;
}
const int N=2005;
int C[N][N],sum[N][N],T,k;
int main(){
    freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);
    splay(T),splay(k);
    C[0][0]=1;
    for(int i=1;i<=2000;i++){
        C[i][0]=1;
        for(int j=1;j<=i;j++){
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%k;
        }
    }
    for(int i=1;i<=2000;i++){
        for(int j=1;j<=2000;j++){
            sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(C[i][j]==0&&i>=j);
        }
    }
    while(T--){
        int n,m;
        splay(n),splay(m);
        printf("%d\n",sum[n][m]);
    }
}

T5

//VEB tree
#include<bits/stdc++.h>
using namespace std;
void splay(int&v){
    char s='a';v=0;int p=0;
    while(s<'0'||s>'9'){s=getchar();if(s=='-')p=1;}
    while(s>='0'&&s<='9')v=v*10+s-'0',s=getchar();
    if(p)v=-v;
}
const int N=7000010;
typedef long long LL;
int n,m,q,u,v,t;
int a[N],b[N],c[N];
int tA,tB,tC,A,B,C;
bool comp(const int&a,const int&b){return a>b;}
int top(){
    int mx=-2147483600;
    if(A<=tA&&a[A]>mx)mx=a[A];
    if(B<=tB&&b[B]>mx)mx=b[B];
    if(C<=tC&&c[C]>mx)mx=c[C];
    if(A<=tA&&a[A]==mx)return a[A++];
    if(B<=tB&&b[B]==mx)return b[B++];
    if(C<=tC&&c[C]==mx)return c[C++];
}
int main(){
    freopen("earthworm.in","r",stdin);
    freopen("earthworm.out","w",stdout);
    splay(n),splay(m),splay(q);
    splay(u),splay(v),splay(t);
    for(int i=1;i<=n;i++){
        splay(a[i]);
    }
    sort(a+1,a+n+1,comp);
    A=B=C=1;tA=n;int fir=1;
    for(int i=1;i<=m;i++){
        int x=top();
        x+=(i-1)*q;
        if(i%t==0){
            if(fir)fir=0;
            else putchar(32);
            printf("%d",x);
        }
        int aa=(LL)x*u/v;
        int bb=x-aa;
        b[++tB]=aa-q*i;
        c[++tC]=bb-q*i;
    }
    puts("");
    fir=1;
    for(int i=1;i<=n+m;i++){
        if(i%t==0){
            if(fir)fir=0;
            else putchar(32);
            printf("%d",top()+q*m);
        }
        else top();
    }
}

T6,这题是搜的

//last OI,last coding
//liuchenrui Noip 2016
//bw&gl&hf
#include<cstdio>
#include<iostream>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
void splay(int&v){
    char s='a';v=0;int p=0;
    while(s<'0'||s>'9'){s=getchar();if(s=='-')p=1;}
    while(s>='0'&&s<='9')v=v*10+s-'0',s=getchar();
    if(p)v=-v;
}
const int N=20;
const double eps=1e-7;
int T,n;
int vis[N],ans,tp;
double a,b,x[N],y[N],A[N][N],B[N][N];
bool eq(double a,double b){return fabs(a-b)<eps;}
void calc(double&a,double&b,double x1,double y1,double x2,double y2){
    double p=x1*x2*x2-x1*x1*x2;
    if(eq(p,0))return a=1,void();
    a=(y2*x1-x2*y1)/p;
    b=(y1-a*x1*x1)/x1;
}
void dfs(int now,int step){
    if(now==n+1){
        ans=min(ans,step);
        return;
    }
    if(step>=ans)return;
    vis[now]=1;
    int s[N];memcpy(s,vis,sizeof s);
    int stk[N],top=0,flag=0;
    for(int i=now+1;i<=n;i++){
        if(!s[i]){
            a=A[i][now];b=B[i][now];
            if(eq(a,0)||a>0)continue;
            top=0;
            for(int j=i;j<=n;j++)if(!s[j]){
                if(eq(x[j]*x[j]*a+x[j]*b,y[j])){
                    s[j]=1;stk[++top]=j;vis[j]=1;
                }
            }
            for(int j=now+1;j<=n+1;j++){
                if(!vis[j]){
                    flag=1;
                    dfs(j,step+1);break;
                }
            }
            for(int j=1;j<=top;j++)vis[stk[j]]=0;
        }
    }
    if(!flag){
        for(int i=now+1;i<=n+1;i++){
            if(!vis[i]){
                dfs(i,step+1);
                break;
            }
        }
    }
    vis[now]=0;
}
int main(){
    freopen("angrybirds.in","r",stdin);
    freopen("angrybirds.out","w",stdout);
    for(cin>>T;T--;){
        cin>>n>>tp;
        for(int i=1;i<=n;i++){
            scanf("%lf%lf",&x[i],&y[i]);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i!=j){
                    calc(A[i][j],B[i][j],x[i],y[i],x[j],y[j]);
                }
            }
        }
        ans=n;
        if(tp==1)ans=(n+2)/3+1;
        dfs(1,0);
        cout<<ans<<endl;
    }
}
阅读更多
想对作者说点什么?
相关热词

博主推荐

换一批

没有更多推荐了,返回首页