# E. Two Platforms

Sep 9th, 2020 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <bits/stdc++.h>
2. using namespace std;
3. #define OO INT_MAX
4.
5. const int N = 2e5 + 5e2;
6. int Memo[N][2][2];
7. vector< int > x;
8. int n ,k ,y;
9.
10. /// ------------- Memo[i][F][S] ------------ ///
11. /// --- i = Index -------------------------- ///
12. /// --- F = If First Platform Used Or Not -- ///
13. /// --- S = If Second Platform Used Or Not - ///
14.
15. int Calc(int i ,bool F ,bool S){
16.     if( i == n ) return 0;
17.
18.     int & Re = Memo[i][F][S];
19.     if( Re  +  1 ) return Re;
20.
21.     Re = -OO;
22.     int EdP    = x[i] + k; /// --- End Position --- //
23.     int newIdx = upper_bound(x.begin() ,x.end() ,EdP) - x.begin();
24.
25.     Re = max(Re ,Calc(i + 1 ,F ,S));                    // Try To Put Platform In i + 1
26.     if( !F ) Re = max(Re ,Calc(newIdx ,true ,S) + (newIdx - i) ); // Try Put F Platform
27.     if( !S ) Re = max(Re ,Calc(newIdx ,F ,true) + (newIdx - i) ); // Try Put S Platform
28.
29.     return Re;
30. }
31.
32. void Solve(){
33.     scanf("%d%d" ,&n ,&k); x.resize(n);
34.     for( auto &i : x ) scanf("%d" ,&i);
35.     for( auto  i : x ) scanf("%d" ,&i);
36.
37.     sort(x.begin() ,x.end());
38.
39.     /// --- Memset(Dp = -1) --- ///
40.     for(int i = 0 ; i <= n ; i++)
41.         for(int F = 0 ; F < 2 ; F++)
42.             for(int S = 0 ; S < 2 ; S++)
43.                 Memo[i][F][S] = -1;
44.
45.     int Ans = Calc(0 ,false ,false);
46.     printf("%d\n" ,Ans);
47. }
48.
49. int main() {
50.     int Tc; scanf("%d" ,&Tc);
51.     while( Tc-- ) Solve();
52.     return 0;
53. }
54.