frp

G

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