frp

G

frp
May 20th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <cstdio>
  2. #include <map>
  3. using namespace std;
  4. typedef unsigned long long ull;
  5. map<ull,int> mem;
  6. unsigned dat[16][16];
  7. unsigned sum,t;
  8. unsigned cs[16];
  9. ull ccs;
  10. inline ull code()
  11. {
  12.     ull res=0;
  13.     for(unsigned i=0;i<t;i++)
  14.         res|=ull(cs[i])<<(4*i);
  15.     return res;
  16. }
  17. int recurse()
  18. {
  19.     ull ccs=code();
  20.     map<ull,int>::iterator it;
  21.     if((it=mem.find(ccs))!=mem.end())return it->second;
  22.     int res=0;
  23.     for(int i=t-1;i>=0;i--)
  24.         if(cs[i]!=0)
  25.             for(int j=i-1;j>=0;j--)
  26.                 if(cs[j]!=0)
  27.                     if(dat[i][cs[i]-1]+dat[j][cs[j]-1]==sum)
  28.                     {
  29.                         cs[i]--;cs[j]--;
  30.                         res=max(res,2+recurse());
  31.                         cs[i]++;cs[j]++;
  32.                     }
  33.     mem.insert(make_pair(ccs,res));
  34.     return res;
  35. }
  36. int main()
  37. {
  38.     int n;scanf("%d%d",&n,&t);
  39.     for(int i=0;i<t;i++)
  40.     {
  41.         int sz=4*n/t+(i<4*n%t?1:0);
  42.         cs[i]=sz;
  43.         for(int j=0;j<sz;j++)
  44.         {
  45.             char c;
  46.             scanf("%d%c",&dat[i][j],&c);
  47.         }
  48.     }
  49.     sum=n+1;
  50.     printf("%d\n",recurse());
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment