/ Randle / 题库 / 杯具 /

题解

1 条题解

  • 0
    @ 2017-08-06 20:01:22
    #include<bits/stdc++.h>
    using namespace std;
    inline const void read(int &a)
    {
    a=0;
    char c=getchar();
    while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')
    {
    a=a*10+c-'0';
    c=getchar();
    }
    }
    int ans=INT_MAX;
    struct NOW
    {
    int n,m;
    bool operator==(const NOW a)
    {
    if(a.n==n&&a.m==m)return true;
    else return false;
    }
    }now[50000];
    int n,m,t,d,k=0;
    bool solve=false;
    int s=0,e=0;
    void dp(int step)
    {
    if(step>t)return ;
    for(int o=s;o<=e;o++)
    {
    int nown=now[o].n,nowm=now[o].m;
    ans=min(abs(nown+nowm-d),ans);
    if(!ans)solve=true;
    if(solve)return ;
    bool yes=true;
    if(nown<n)
    {
    now[k+1].n=n;
    now[k+1].m=nowm;
    for(int i=0;i<=k;i++)
    if(now[k+1]==now[i]){yes=false;break;}
    if(yes)k++;
    }
    yes=true;
    if(nowm<m)
    {
    now[k+1].m=m;
    now[k+1].n=nown;
    for(int i=0;i<=k;i++)
    if(now[k+1]==now[i]){yes=false;break;}
    if(yes)k++;
    }
    yes=true;
    if(nown)
    {
    now[k+1].n=0;
    now[k+1].m=nowm;
    for(int i=0;i<=k;i++)
    if(now[k+1]==now[i]){yes=false;break;}
    if(yes)k++;
    }
    yes=true;
    if(nowm)
    {
    now[k+1].m=0;
    now[k+1].n=nown;
    for(int i=0;i<=k;i++)
    if(now[k+1]==now[i]){yes=false;break;}
    if(yes)k++;
    }
    yes=true; 
    if(nown>0&&nowm<m)
    {
    if(nown+nowm<=m)
    {
    now[k+1].n=0;
    now[k+1].m=nown+nowm;
    }
    else
    {
    now[k+1].n=nown+nowm-m;
    now[k+1].m=m;
    }
    for(int i=0;i<=k;i++)
    if(now[k+1]==now[i]){yes=false;break;}
    if(yes)k++;
    }
    yes=true;
    if(nowm>0&&nown<n)
    {
    if(nown+nowm<=n)
    {
    now[k+1].n=nown+nowm;
    now[k+1].m=0;
    }
    else
    {
    now[k+1].n=n;
    now[k+1].m=nown+nowm-n;
    }
    for(int i=0;i<=k;i++)
    if(now[k+1]==now[i]){yes=false;break;}
    if(yes)k++;
    }
    yes=true;
    }
    s=e+1;
    e=k;
    dp(step+1);
    }
    int main()
    {
    read(n);read(m);read(t);read(d);
    now[0].n=now[0].m=0;
    dp(0);
    printf("%d",ans);
    return 0;
    }
    
  • 1

信息

难度
9
分类
(无)
标签
(无)
递交数
2
已通过
2
通过率
100%
上传者