【NOIP2017】Day2代码

T1 cheese

#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;
const double eps=1e-10;
const int MAXN=1005;
const int MAXM=1005*1005;
int n,h,r,S,T,Q,tot;
int head[MAXN];
bool vis[MAXN];
double d,xx,yy,zz,ret;
queue<int>q;

struct Tball
{
    int x,y,z;
};
Tball ba[MAXN];

struct Tway
{
    int u,v,nex;
};
Tway e[MAXM<<1];

inline LL sqr(int x)
{
    return 1ll*x*x;
}

inline double get_dis(int A,int B)
{
    xx=(double)sqr(ba[A].x-ba[B].x);
    yy=(double)sqr(ba[A].y-ba[B].y);
    zz=(double)sqr(ba[A].z-ba[B].z);
    ret=(double)xx+yy+zz;ret=sqrt(ret);
    return ret;
}

inline void _reset()
{
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    tot=0;
}

inline void add(int u,int v)
{
    ++tot;
    e[tot].u=u;e[tot].v=v;
    e[tot].nex=head[u];head[u]=tot;
}

inline void init()
{
    scanf("%d%d%d",&n,&h,&r);
    d=2.0*(double)r;
    S=0;T=n+1;
    for(int i=1;i<=n;++i)
    {
        scanf("%d%d%d",&ba[i].x,&ba[i].y,&ba[i].z);
        if(ba[i].z<=r)
        {
            add(S,i);
            add(i,S);
        }
        if(h-ba[i].z<=r)
        {
            add(i,T);
            add(T,i);
        }
    }

    double tmp;
    for(int i=1;ifor(int j=i+1;j<=n;++j)
        {
            tmp=get_dis(i,j);
            if(tmp-d>eps)
                continue;
            add(i,j);
            add(j,i);
        }
}

inline void solve()
{
    while(!q.empty())
        q.pop();
    q.push(S);vis[S]=true;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].nex)
        {
            int v=e[i].v;
            if(!vis[v])
            {
                vis[v]=true;
                q.push(v);
            }
        }
    }
    if(vis[T])
        printf("Yes\n");
    else
        printf("No\n");
}

int main()
{
    freopen("cheese.in","r",stdin);
    freopen("cheese.out","w",stdout);

    scanf("%d",&Q);
    while(Q--)
    {
        _reset();
        init();
        solve();
    }

    return 0;
}

T2 treasure

#include
#include
#include
#include
using namespace std;

const int INF=0x7fffffff;
const int MAXN=13;
const int MAXM=1010;

int n,m,tot,ans;
int head[MAXN];
int f[MAXN][1<1<int v,w,nex;
    Tway(){}
    Tway(int vv,int ww,int nexx){v=vv;w=ww;nex=nexx;}
};
Tway e[MAXM<<1];

inline void add(int u,int v,int w)
{
    e[++tot]=Tway(v,w,head[u]);
    head[u]=tot;
}

int main()
{
    freopen("treasure.in","r",stdin);
    freopen("treasure.out","w",stdout);

    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(x==y)
            continue;
        add(x,y,z);add(y,x,z);
    }

    memset(f,0x3f,sizeof(f));
    for(int i=1;i<=n;++i)
    {
        f[i][1<<(i-1)]=0;
        dep[i][1<<(i-1)][i]=1;
    }
    for(int i=1;i<=n;++i)
    {
        for(int sta=1;sta<(1<if( (sta&(1<<(i-1)))==0 || sta==(1<<(i-1)))
                continue;

            int tmpsta,tmpv,tmpu,tmp=INF;
            for(int j=1;j<=n;++j)
            {
                if(j==i || (sta&(1<<(j-1)))==0 )
                    continue;
                for(int k=head[j];k;k=e[k].nex)
                {
                    int tk=e[k].w*dep[i][sta-(1<<(j-1))][e[k].v]+f[i][sta-(1<<(j-1))];
    //              printf("%d %d\n",e[k].w,dep[i][sta-(1<<(j-1))][e[k].v]);
                    if(tk1<<(e[k].v-1)))!=0 )
                    {
                        tmp=tk;
                        tmpsta=sta-(1<<(j-1));
                        tmpv=e[k].v;
                        tmpu=j;
                    }
//                  printf("%d %d %d\n",e[k].v,e[k].w,e[k].nex);
//                  printf("%d %d %d\n",tmpsta,tmpv,tmpu);
                }
            }
//          printf("%d %d %d\n",tmpsta,tmpv,tmpu);
            if(tmp==INF)
                continue;
            f[i][sta]=tmp;
            memcpy(dep[i][sta],dep[i][tmpsta],sizeof(dep[i][sta]));
            dep[i][sta][tmpu]=dep[i][sta][tmpv]+1;      
        }
    }
    ans=INF;
    for(int i=1;i<=n;++i)
        ans=min(ans,f[i][(1<1]);
    printf("%d\n",ans);

    return 0;
}

T3 phalanx

#include
#include
#include
#include
using namespace std;

typedef long long LL;
const int MAXN=1e7+10;
int tot,pos,n,m,q;
int siz[MAXN],rt[MAXN],len[MAXN];
int tr[MAXN][2];
LL key[MAXN];

inline void update(int x)
{
    siz[x]=siz[tr[x][0]]+siz[tr[x][1]];
}

inline LL get_k(int &root,int l,int r,int k)
{
    if(!root)
        root=++tot;
    if(l==r)
    {
        siz[root]=1;pos=l;
        return key[root];
    }
    int mid=(l+r)>>1,tp=mid-l+1-siz[tr[root][0]];
    LL ret;
    if(tp>=k)
        ret=get_k(tr[root][0],l,mid,k);
    else
        ret=get_k(tr[root][1],mid+1,r,k-tp);
    update(root);
    return ret;
}

inline void change(int &root,int l,int r,int x,LL y)
{
    if(!root)
        root=++tot;
    if(l==r)
    {
        key[root]=y;
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid)
        change(tr[root][0],l,mid,x,y);
    else
        change(tr[root][1],mid+1,r,x,y);
    update(root);
}

int main()
{
    freopen("phalanx.in","r",stdin);
    freopen("phalanx.out","w",stdout);

    scanf("%d%d%d",&n,&m,&q);
    for(int i=1;i<=n;++i)
        len[i]=m-1;
    len[n+1]=n;

    for(int i=1;i<=q;++i)
    {
        int x,y;
        scanf("%d%d",&x,&y);

        if(y==m)
        {
            LL num=get_k(rt[n+1],1,n+q,x);
            if(pos<=n)
                num=1ll*pos*m;
            printf("%lld\n",num);
            change(rt[n+1],1,n+q,++len[n+1],num);
        }
        else
        {
            LL num=get_k(rt[x],1,m+q,y);
            if(pos<m)
                num=1ll*(x-1)*m+pos;
            printf("%lld\n",num);

            change(rt[n+1],1,n+q,++len[n+1],num);
            num=get_k(rt[n+1],1,n+q,x);
            if(pos<=n)
                num=1ll*pos*m;
            change(rt[x],1,m+q,++len[x],num);
        }
    }
    return 0;
}

你可能感兴趣的:(NOIP)