cfER76 abcd

https://codeforces.com/contest/1257

A題 取個max(b-a+x, n-1)

B題 儅x>3 時就可以變成任意y, 儅 n <= 3 時 討論下

C題 用數組記錄下沒個數的出現位置, 取與上一次位置的最小距離為答案

D題 儅怪物中力量最大的大於勇士 則輸出 -1, 否則 貪心 將每個耐力值取最大力量, 用一個數組存  耐力值的最大力量()

/*
6
1 1 1 2 2 2
*/

#include <bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)
#define ll long long
void taskA(){
    int t, n,x,a,b;
    cin >> t;
    while(t--) {
        cin >> n >> x >> a >> b;
        if(a > b) swap(a, b);
        int ma = b-a;
        if(abs(b-a) < n) 
            ma = min(n-1, ma+x);
        cout << ma << endl;
    }
}
void taskB(){
    ll t, x, y;
    cin >> t;
    while(t--) {
        int flag = 1;
        cin >> x >> y;
        //if(x%2==0 && x/2*3==x && x!=y) flag = 0;
        if(((x==3 && y>3) || (x==1 && y>1) || (x==2 && y>3))) 
            flag = 0;
        if(flag) cout << "YES\n";
        else cout << "NO\n";
    }
}
void taskC(){
    int t; cin >> t;
    while(t--) {
        int n; cin >> n;
        int ma = 0, k, ans = n+5, x;
        vector<int> lst(n+1, 0);
        _rep(i,1,n) {
            cin >> x;
            if(lst[x])  ans = min(ans, i-lst[x]+1);
            lst[x] = i;
        } if(ans > n) ans = -1;
        cout << ans << endl;
    }
}
void taskD(){
    int t; cin>> t;
    while(t--) {
        int n; cin >> n;
        //vector<int> mon, hero;
        vector<int> mon(n), hero(n+1, 0);
        int mxm = 0;
        _for(i,0,n) {
            cin >> mon[i];
            mxm = max(mon[i], mxm);
        }
        int m, mxp = 0; cin >> m;

        _for(i,0,m) {
            int x,y;
            cin >> x >> y;
            mxp = max(mxp, x);
            hero[y] = max(hero[y], x);
        }
        if(mxp < mxm) {
            cout<<"-1\n"; continue;
        }
        for(int i = n-1; i > 0; --i) 
            hero[i] = max(hero[i], hero[i+1]);

        int ans = 0, pos = 0;
        while(pos < n) {
            ans++;
            int npos = pos;
            int mx = 0;
            while(1) {
                mx = max(mx, mon[npos]);
                if(mx > hero[npos-pos+1]) break;
                npos++;
            }
            //if(npos == pos) break;
            pos = npos;
        }
        cout << ans << endl;
    }
}  
int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    //freopen("output.txt", "w", stdout);
    //taskA();
    //taskB();
    //taskC();
    taskD();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/163467wyj/p/11870076.html