Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define rep(i, f, t) for (auto i = f; i < t; ++i)
- #define read(a, n) for (int i = 0; i < n; ++i) cin >> a[i];
- #define sz(x) (int)(x.size())
- #define all(x) x.begin(), x.end()
- #define rall(x) x.rbegin(), x.rend()
- #define int long long
- #define ll long long
- #define pii pair<int, int>
- const ll mod = (ll) (1e9 + 7);
- const int inf = (int) (2e9);
- const ll linf = (ll) (4e18);
- void solve();
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- cout << fixed << setprecision(20);
- solve();
- return 0;
- }
- const int N = (int) (2 * 1e5 + 10);
- int a[N];
- pii b[N];
- pii mrak[N];
- bool comp(pii &a, pii &b){
- if (a.first == b.first) return a.second < b.second;
- return a.first < b.first;
- }
- void solve() {
- int q;
- cin >> q;
- while (q--){
- int n, m;
- cin >> n;
- read(a, n);
- int mx = a[0];
- rep(i, 1, n){
- mx = max(mx, a[i]);
- }
- bool can = false;
- cin >> m;
- rep(i, 0, m){
- cin >> b[i].first >> b[i].second;
- if (b[i].first >= mx) can = true;
- }
- if (!can){
- cout << -1 << "\n";
- continue;
- }
- sort(b, b + m, comp);
- mrak[m - 1] = {b[m - 1].second, m - 1};
- for (int i = m - 2; i >= 0; --i){
- mrak[i] = mrak[m + 1];
- if (b[i].second > mrak[i].first) mrak[i].first = b[i].second, mrak[i].second = i;
- }
- int cnt = 0;
- int pos1 = 0, pos2 = 0;
- int tmp = 0;
- while (pos1 < n){
- if (b[pos2].first < a[pos1]){
- if (tmp > b[pos2 + 1].second){
- cnt += tmp / mrak[pos2].first;
- tmp = tmp % mrak[pos2].first;
- pos2 = mrak[pos2].second;
- continue;
- }
- else {
- int j = pos2 + 1;
- while (b[j].first < a[pos1]) j++;
- pos2 = j;
- continue;
- }
- }
- if (tmp + 1 <= b[pos2].second){
- pos1++;
- tmp++;
- }
- else {
- cnt++;
- tmp = 0;
- }
- }
- if (tmp > 0) cnt++;
- cout << cnt << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement