Advertisement
istinishat

Bitmask

May 31st, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. /*
  2. problem Codeforces 306 (div 2) B
  3. */
  4.  
  5. #include<iostream>
  6. #include<cstdio>
  7. #include<algorithm>
  8. using namespace std;
  9.  
  10. #define si(n) scanf("%d",&n)
  11.  
  12. int main()
  13. {
  14.     int n,l,r,x,ans=0;
  15.     si(n);si(l);si(r);si(x);
  16.  
  17.     int arr[n],i;
  18.     for(i=0;i<n;i++)si(arr[i]);
  19.  
  20.     int tot=(1<<n),j;
  21.     for(i=0;i<tot;i++){
  22.         if(__builtin_popcount(i)<2)
  23.             continue;
  24.         int mn=1000000000,mx=-1000000000,jog=0;
  25.         for(j=0;j<n;j++){
  26.             if(i&(1<<j)){
  27.                 mn=min(mn,arr[j]);
  28.                 mx=max(mx,arr[j]);
  29.                 jog+=arr[j];
  30.             }
  31.         }
  32.         if(mx-mn>=x && jog>=l && jog<=r)
  33.             ans++;
  34.     }
  35.  
  36.     cout<<ans<<endl;
  37.  
  38.     return 0;
  39.  
  40. }
  41.  
  42. /*
  43. lightoj : 1011 - Marriage Ceremonies
  44. dp with bitmask
  45. */
  46.  
  47. #include<iostream>
  48. #include<cstdio>
  49. #include<cstring>
  50. using namespace std;
  51.  
  52. #define si(n) scanf("%d",&n)
  53.  
  54. int arr[20][20],n,dp[(1<<17)][20];
  55.  
  56. int go(int r,int msk){
  57.     int i;
  58.     if(r==n)
  59.         return 0;
  60.     int &ref=dp[msk][r];
  61.     if(ref != -1)
  62.         return ref;
  63.     for(i=0;i<n;i++){
  64.         if(!(msk & (1<<i))){
  65.             ref=max(ref,go(r+1,msk^(1<<i))+arr[r][i]);
  66.         }
  67.     }
  68.     return ref;
  69. }
  70.  
  71. int main()
  72. {
  73.     int t,i,j;
  74.     //cout<<(1<<20)<<endl;
  75.     //freopen("input.txt","r",stdin);
  76.     si(t);
  77.  
  78.     for(int cs=1;cs<=t;cs++){
  79.         memset(dp,-1,sizeof(dp));
  80.         si(n);
  81.         for(i=0;i<n;i++){
  82.             for(j=0;j<n;j++)
  83.                 si(arr[i][j]);
  84.         }
  85.         int ans=go(0,0);
  86.         printf("Case %d: %d\n",cs,ans);
  87.  
  88.     }
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement