Advertisement
shamiul93

1017 - Brush (III)

Jun 25th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. /**
  2. @author - Rumman BUET CSE'15
  3. Problem - 1017 - Brush (III)
  4.  
  5. Concept - DP , Knapsack (may be)...
  6. Idea - Once you reach to an element , you have to start the range from here and then you check all
  7. those elements who are less than w distance from it by a for loop. then you call a recursive function
  8. and you get the answer. See the function.
  9. **/
  10. // LightOJ always needs this format for sure..so I made a copy of it...
  11. #include <bits/stdc++.h>
  12. #include<vector>
  13. #define ll                                      long long int
  14. #define fi                                      freopen("in.txt", "r", stdin)
  15. #define fo                                      freopen("out.txt", "w", stdout)
  16. #define m0(a) memset(a , 0 , sizeof(a))
  17. #define m1(a) memset(a , -1 , sizeof(a))
  18. #define inf LLONG_MAX
  19. #define min3(a,b,c) min(a,min(b,c))
  20. #define max3(a,b,c) max(a,max(b,c))
  21. #define ones(mask)  __builtin_popcount(mask) /// __builtin_popcount : it's a built in function of GCC. Finds out the numbers of 1 in binary representation.
  22. #define mx 150000
  23.  
  24. using namespace std;
  25.  
  26.  
  27. ll N , w , k , len ;
  28. vector<ll> v ;
  29. ll dp[102][102] ;
  30.  
  31. ll solve(ll pos , ll moves)
  32. {
  33.     if(pos >= len)
  34.     {
  35.         return 0 ;
  36.     }
  37.     if(moves == 0)
  38.     {
  39.         return 0 ;
  40.     }
  41.     if(dp[pos][moves] != -1)
  42.     {
  43.         return dp[pos][moves] ;
  44.     }
  45.  
  46.     ll t = 0 , i ;
  47.     for( i = 0 ; i < v.size() - pos ; i++)
  48.     {
  49.         if(abs(v[pos + i] - v[pos]) <= w)
  50.         {
  51.             t++ ;
  52.         }
  53.         else
  54.         {
  55.             break ;
  56.         }
  57.     }
  58.  
  59.     return dp[pos][moves] = max( t + solve(pos+i , moves-1) , solve(pos+1 , moves)) ;
  60. }
  61.  
  62. int main()
  63. {
  64. //    fi ;
  65. //    fo ;
  66.     ll T , t = 0 ;
  67.     scanf("%lld",&T) ;
  68.  
  69.     while(T--)
  70.     {
  71.         v.clear() ;
  72.         m1(dp) ;
  73.         t++ ;
  74.         cin >> N >> w >> k ;
  75.         ll x , y ;
  76.  
  77.         for(ll i = 0 ; i < N ; i++)
  78.         {
  79.             cin >> x >> y ;
  80.             v.push_back(y);
  81.         }
  82.  
  83.         sort(v.begin() , v.end()) ;
  84.         len = v.size()  ;
  85.  
  86.         printf("Case %lld: ",t) ;
  87.         cout << solve(0 , k) << endl ;
  88.     }
  89.     return 0 ;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement