Madiyar

Untitled

Apr 11th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <vector>
  8. #include <map>
  9. #include <set>
  10. #include <ctime>
  11. #include <cassert>
  12. #include <queue>
  13.  
  14. using namespace std;
  15.  
  16. #define f first
  17. #define s second
  18. #define mp make_pair
  19. #define pb push_back
  20. #define forit(it,con) for (typeof(con.begin()) it = con.begin(); it != con.end(); ++it)
  21. #define f0(a) memset(a, 0, sizeof(a))
  22. #define all(v) v.begin(), v.end()
  23. #define pii pair<int,int>
  24. #define vi vector<int>
  25. #define ll long long
  26. #ifdef WIN32
  27.     #define I64 "%I64d"
  28. #else
  29.     #define I64 "%lld"
  30. #endif
  31. const int maxn = (int)1e6;
  32. const int inf = (int)2e9;
  33.  
  34. int type[maxn], h[maxn], last[maxn], st[maxn], wh[maxn], dp[maxn];
  35. int T[maxn * 2];
  36. int n, stn;
  37.  
  38.  
  39. void update(int v, int x) {
  40.     v += n;
  41.     T[v] = x;
  42.     while (v > 1) {
  43.         v >>= 1;
  44.         T[v] = min(T[v + v], T[v + v + 1]);
  45.     }
  46. }
  47.  
  48. int findMax(int l, int r) {
  49.     l += n; r += n;
  50.     int res = inf;
  51.     while (l <= r) {
  52.         if (l & 1) res = min(res, T[l]);
  53.         if (!(r & 1)) res = min(res, T[r]);
  54.         l = (l + 1) >> 1; r = (r - 1) >> 1;
  55.     }
  56.     return res;
  57. }
  58.  
  59.  
  60. void Solve() {
  61.     scanf("%d", &n);
  62.     for (int i = 1; i <= n; ++i)
  63.         scanf("%d%d", &type[i], &h[i]);
  64.  
  65.     for (int i = 1; i <= n + n + 10; ++i)
  66.         T[i] = inf;
  67.  
  68.     int le = 1;
  69.     stn = 0;
  70.     dp[0] = 0;
  71.  
  72.     for (int i = 1; i <= n; ++i) {
  73.         if (last[type[i]] && le < last[type[i]])
  74.             le = last[type[i]];
  75.        
  76.         while (stn > 0 && h[st[stn - 1]] < h[i]) {
  77.             update(wh[st[stn - 1]], inf);
  78.             --stn;     
  79.         }  
  80.    
  81.         dp[i] = findMax(le - 1, i - 1);
  82.         int pos = upper_bound(st, st + stn, le) - st;
  83.            
  84.         wh[stn] = st[stn - 1];
  85.         st[stn++] = i;
  86.         update(wh[stn - 1], dp[wh[stn - 1]] + h[i]);
  87.         last[type[i]] = i;         
  88.     }
  89.  
  90.     for (int i = 1; i <= n; ++i)
  91.         last[type[i]] = 0;
  92. }
  93.  
  94. int main() {
  95.     #ifdef LOCAL
  96.         freopen("in","r",stdin);
  97.         freopen("out","w",stdout);
  98.     #endif
  99.     int tests;
  100.     scanf("%d", &tests);
  101.     for (int test = 1; test <= tests; ++test) {
  102.         printf("Case %d: ", test);
  103.         Solve();
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment