Advertisement
Guest User

chpok

a guest
Nov 13th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define rep(i, f, t) for (auto i = f; i < t; ++i)
  6. #define read(a, n) for (int i = 0; i < n; ++i) cin >> a[i];
  7. #define sz(x) (int)(x.size())
  8. #define all(x) x.begin(), x.end()
  9. #define rall(x) x.rbegin(), x.rend()
  10. #define int long long
  11. #define ll long long
  12. #define pii pair<int, int>
  13.  
  14. const ll mod = (ll) (1e9 + 7);
  15. const int inf = (int) (2e9);
  16. const ll linf = (ll) (4e18);
  17.  
  18. void solve();
  19.  
  20. signed main() {
  21.     ios::sync_with_stdio(false);
  22.     cin.tie(nullptr);
  23.     cout.tie(nullptr);
  24.     cout << fixed << setprecision(20);
  25.     solve();
  26.     return 0;
  27. }
  28.  
  29. const int N = (int) (2 * 1e5 + 10);
  30.  
  31. int a[N];
  32. pii b[N];
  33. pii mrak[N];
  34.  
  35. bool comp(pii &a, pii &b){
  36.     if (a.first == b.first) return a.second < b.second;
  37.     return a.first < b.first;
  38. }
  39.  
  40. void solve() {
  41.     int q;
  42.     cin >> q;
  43.     while (q--){
  44.         int n, m;
  45.         cin >> n;
  46.         read(a, n);
  47.         int mx = a[0];
  48.         rep(i, 1, n){
  49.             mx = max(mx, a[i]);
  50.         }
  51.         bool can = false;
  52.         cin >> m;
  53.         rep(i, 0, m){
  54.             cin >> b[i].first >> b[i].second;
  55.             if (b[i].first >= mx) can = true;
  56.         }
  57.         if (!can){
  58.             cout << -1 << "\n";
  59.             continue;
  60.         }
  61.         sort(b, b + m, comp);
  62.         mrak[m - 1] = {b[m - 1].second, m - 1};
  63.         for (int i = m - 2; i >= 0; --i){
  64.             mrak[i] = mrak[m + 1];
  65.             if (b[i].second > mrak[i].first) mrak[i].first = b[i].second, mrak[i].second = i;
  66.         }
  67.         int cnt = 0;
  68.         int pos1 = 0, pos2 = 0;
  69.         int tmp = 0;
  70.         while (pos1 < n){
  71.             if (b[pos2].first < a[pos1]){
  72.                 if (tmp > b[pos2 + 1].second){
  73.                     cnt += tmp / mrak[pos2].first;
  74.                     tmp = tmp % mrak[pos2].first;
  75.                     pos2 = mrak[pos2].second;
  76.                     continue;
  77.                 }
  78.                 else {
  79.                     int j = pos2 + 1;
  80.                     while (b[j].first < a[pos1]) j++;
  81.                     pos2 = j;
  82.                     continue;
  83.                 }
  84.             }
  85.             if (tmp + 1 <= b[pos2].second){
  86.                 pos1++;
  87.                 tmp++;
  88.             }
  89.             else {
  90.                 cnt++;
  91.                 tmp = 0;
  92.             }
  93.         }
  94.         if (tmp > 0) cnt++;
  95.         cout << cnt << "\n";
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement