【noip2015】d1解题报告

t1:神奇的幻方,送分模拟

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,mmp[45][45],lx,ly;
int main()
{
    scanf("%d",&n);
    mmp[1][(n/2)+1]=1;
    lx=1,ly=(n/2)+1;
    for(int i=2;i<=n*n;i++)
    {
        if(lx==1&&ly<n)
            mmp[n][++ly]=i,lx=n;
        else if(lx>1&&ly==n)
            mmp[--lx][1]=i,ly=1;
        else if(lx==1&&ly==n)
            mmp[++lx][ly]=i;
        else if(lx>1&&ly<n)
        {
            if(mmp[lx-1][ly+1]==0)
                mmp[--lx][++ly]=i;
            else
                mmp[++lx][ly]=i;
        }
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d",mmp[i][1]);
        for(int j=2;j<=n;j++)
            printf(" %d",mmp[i][j]);
        printf("\n");
    }
}

t2:tarjan版子,找最小环

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int n,lin,tail,tot,ci,ans=1000000007;
struct in
{
    int to,ne;
}ter[200020];
int head[200020],scc[200020],si[200020],low[200020],dfn[200020];
stack<int>qwq;
inline void re(int &a)
{
    a=0;
    char b=getchar();
    while(b<'0'||b>'9')
        b=getchar();
    while(b>='0'&&b<='9')
        a=a*10+b-'0',b=getchar();
}
inline void build(int f,int l)
{
    ter[++tail]=(in){l,head[f]},head[f]=tail;
}
void dfs(int x)
{
    dfn[x]=low[x]=++ci;
    qwq.push(x);
    for(int i=head[x];i>0;i=ter[i].ne)
    {
        int t=ter[i].to;
        if(!dfn[t])
        {
            dfs(t);
            low[x]=min(low[x],low[t]);
        }
        else if(!scc[t])
            low[x]=min(low[x],dfn[t]);
    }
    if(dfn[x]==low[x])
    {
        tot++;
        int s=0;
        while(1)
        {
            int u=qwq.top();
            qwq.pop();
            scc[u]=tot;
            s++;
            if(u==x)
            {
                si[tot]=s;break;
            }
        }
    }
}
int main()
{
    re(n);
    for(int i=1;i<=n;i++)
        re(lin),build(i,lin);
    for(int i=1;i<=n;i++)
        if(!scc[i])
            dfs(i);
    for(int i=1;i<=tot;i++)
    {
        if(si[i]>1)
            ans=min(ans,si[i]);
    }
    printf("%d",ans);
}

t3:斗地主,大爆搜,被cogld的爆搜所惊艳到了,好简单……为啥我就没想到呢

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int t,n,shu[20],ans,x,y;
void dfs(int x)
{
    int c1=0,c2=0,c3=0,c4=0;
    for(int i=1;i<=14;i++)
    {
        if(shu[i]==1)
            c1++;
        else if(shu[i]==2)
            c2++;
    }
    for(int i=1;i<=14;i++)
    {
        if(shu[i]!=3)
            continue;
        c3++;
        if(c1>=1)
            c1--;
        else if(c2>=1)
            c2--;
    }
    for(int i=1;i<=14;i++)
    {
        if(shu[i]!=4)
            continue;
        c4++;
        if(c1>=2)
            c1-=2;
        else if(c2>=2)
            c2-=2;
        else if(c2>=1)
            c2--;
    }
    ans=min(ans,x+c1+c2+c3+c4);
    for(int i=1;i<=8;i++)
    {
        int j;
        for(j=i;j<=12;j++)
        {
            if(shu[j]<1)
                break;
            shu[j]--;
            if(j-i>=4)
                dfs(x+1);
        }
        for(int k=i;k<=j-1;k++)
            shu[k]++;
    }
    for(int i=1;i<=10;i++)
    {
        int j;
        for(j=i;j<=12;j++)
        {
            if(shu[j]<2)
                break;
            shu[j]-=2;
            if(j-i>=2)
                dfs(x+1);
        }
        for(int k=i;k<=j-1;k++)
            shu[k]+=2;
    }
    for(int i=1;i<=11;i++)
    {
        int j;
        for(j=i;j<=12;j++)
        {
            if(shu[j]<3)
                break;
            shu[j]-=3;
            if(j-i>=1)
                dfs(x+1);
        }
        for(int k=i;k<=j-1;k++)
            shu[k]+=3;
    }
    return; 
}
int main()
{
    //2freopen("qwq.out","w",stdout);
    scanf("%d%d",&t,&n);
    while(t--)
    {
        ans=n;
        memset(shu,0,sizeof(shu));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            if(x==0)
                shu[14]++;
            else if(x==1)
                shu[12]++;
            else if(x==2)
                shu[13]++;
            else
                shu[x-2]++;
        }
        dfs(0);
        cout<<ans<<'\n';
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}

 

posted @ 2017-11-10 07:15  那一抹落日的橙  阅读(339)  评论(0编辑  收藏  举报