poj 1020 Anniversary Cake

// poj 1020 Anniversary Cake
#include <iostream>
#include <algorithm>
using namespace std;

int n,s,a[11],d[41];
bool flag;
void dfs(int step)
{
     if (flag) return ;
     if (step==0) { flag=true; return ;}
     int j=0;
     for (int i=1;i<=s;i++) if (d[i]>d[j])  j=i;
     for (int i=10;i>=1;i--)
     if (a[i]>0 && j+i-1<=s && i<=d[j])
     {
                  int k;
                  for (k=j;k<=j+i-1;k++) if (d[j]>d[k]) break;
                  if (k<=j+i-1) continue;
                  a[i]--;
                  for (k=j;k<=j+i-1;k++) d[k]-=i;
                  dfs(step-1);
                  a[i]++;
                  for (k=j;k<=j+i-1;k++) d[k]+=i;                 
     }
}
 
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
          scanf("%d%d",&s,&n);
          int sum=0,b;
          memset(a,0,sizeof(a));
          for (int i=1;i<=n;i++)
          {
              scanf("%d",&b);
              a[b]++;
              sum+=b*b;
          }
          if (sum!=s*s)
          {
                       printf("HUTUTU!/n");
                       continue;
          }
         
          for (int i=1;i<=s;i++) d[i]=s;
          d[0]=0;
          flag=false;
         
          dfs(n);
       
          if (flag) printf("KHOOOOB!/n");
          else printf("HUTUTU!/n");
    }
    system("pause");
    return 0;
}

 

你可能感兴趣的:(poj 1020 Anniversary Cake)