Saleh127

UVA 1240 / Bitmask DP

Jan 27th, 2022 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. /***
  2.  created: 2022-01-27-19.27.38
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. ll Set(ll N,ll pos)
  13. {
  14.     return N=N |(1<<pos);
  15. }
  16. ll reset(ll N,ll pos)
  17. {
  18.     return N= N & ~(1<<pos);
  19. }
  20. bool check(ll N,ll pos)
  21. {
  22.     return (bool)(N & (1<<pos));
  23. }
  24. ll n,a[16][16];
  25. ll dp[5][(1<<12)+5][310];
  26.  
  27. ll solve(ll mask,ll in,ll z)
  28. {
  29.     if(mask==(1<<n)-1) return 0;
  30.  
  31.     if(dp[in][mask][z]!=-1) return dp[in][mask][z];
  32.  
  33.     ll ans=0;
  34.  
  35.     for(ll i=0; i<n; i++)
  36.     {
  37.         if(check(mask,i)==0)
  38.         {
  39.              if(z+a[1][i]<=280 && in!=1)
  40.              {
  41.                   ans=max(ans,1+solve(Set(mask,i),1,z+a[1][i]));
  42.              }
  43.              if(z+a[2][i]<=280 && in!=2)
  44.              {
  45.                   ans=max(ans,1+solve(Set(mask,i),2,z+a[2][i]));
  46.              }
  47.              if(z+a[3][i]<=280 && in!=3)
  48.              {
  49.                   ans=max(ans,1+solve(Set(mask,i),3,z+a[3][i]));
  50.              }
  51.         }
  52.     }
  53.  
  54.     return dp[in][mask][z]=ans;
  55. }
  56.  
  57. int main()
  58. {
  59.     ios_base::sync_with_stdio(0);
  60.     cin.tie(0);
  61.     cout.tie(0);
  62.  
  63.  
  64.     test
  65.     {
  66.         cin>>n;
  67.  
  68.         for(ll i=1; i<=3; i++)
  69.         {
  70.             for(ll j=0; j<n; j++)
  71.             {
  72.                 cin>>a[i][j];
  73.             }
  74.         }
  75.  
  76.         memset(dp,-1,sizeof dp);
  77.  
  78.         cout<<solve(0,0,0)<<nl;
  79.     }
  80.  
  81.  
  82.     get_lost_idiot;
  83. }
  84.  
  85. /*
  86.  
  87. #include <bits/stdc++.h>
  88. using namespace std;
  89. #define ll long long
  90. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  91. #define get_lost_idiot return 0
  92. #define nl '\n'
  93.  
  94. ll Set(ll N,ll pos)
  95. {
  96.     return N=N |(1<<pos);
  97. }
  98. ll reset(ll N,ll pos)
  99. {
  100.     return N= N & ~(1<<pos);
  101. }
  102. bool check(ll N,ll pos)
  103. {
  104.     return (bool)(N & (1<<pos));
  105. }
  106. ll n,a[16][16];
  107. ll dp[5][(1<<12)+5][310];
  108.  
  109. ll solve(ll mask,ll in,ll z)
  110. {
  111.     if(mask==(1<<n)-1) return 0;
  112.  
  113.     if(dp[in][mask][z]!=-1) return dp[in][mask][z];
  114.  
  115.     ll ans=0;
  116.  
  117.     for(ll i=0; i<n; i++)
  118.     {
  119.         if(check(mask,i)==0)
  120.         {
  121.             for(ll j=0; j<3; j++)
  122.             {
  123.                 if(in==j || z+a[j][i]>280) continue;
  124.  
  125.                 ans=max(ans,1+solve(Set(mask,i),j,z+a[j][i]));
  126.  
  127.             }
  128.         }
  129.     }
  130.  
  131.     return dp[in][mask][z]=ans;
  132. }
  133.  
  134. int main()
  135. {
  136.     ios_base::sync_with_stdio(0);
  137.     cin.tie(0);
  138.     cout.tie(0);
  139.  
  140.  
  141.     test
  142.     {
  143.         cin>>n;
  144.  
  145.         for(ll i=0; i<3; i++)
  146.         {
  147.             for(ll j=0; j<n; j++)
  148.             {
  149.                 cin>>a[i][j];
  150.             }
  151.         }
  152.  
  153.         memset(dp,-1,sizeof dp);
  154.  
  155.         cout<<solve(0,0,0)<<nl;
  156.     }
  157.  
  158.  
  159.     get_lost_idiot;
  160. }
  161.  
  162. */
Add Comment
Please, Sign In to add comment