• Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem


    You play a computer game. In this game, you lead a party of mm heroes, and you have to clear a dungeon with nn monsters. Each monster is characterized by its power aiai. Each hero is characterized by his power pipi and endurance sisi.

    The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.

    When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated kk monsters, the hero fights with the monster k+1k+1). When the hero fights the monster, there are two possible outcomes:

    • if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends;
    • otherwise, the monster is defeated.

    After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the ii-th hero cannot defeat more than sisimonsters during each day), or if all monsters are defeated — otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.

    Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times.

    Input

    The first line contains one integer tt (1t1051≤t≤105) — the number of test cases. Then the test cases follow.

    The first line of each test case contains one integer nn (1n21051≤n≤2⋅105) — the number of monsters in the dungeon.

    The second line contains nn integers a1a1, a2a2, ..., anan (1ai1091≤ai≤109), where aiai is the power of the ii-th monster.

    The third line contains one integer mm (1m21051≤m≤2⋅105) — the number of heroes in your party.

    Then mm lines follow, each describing a hero. Each line contains two integers pipi and sisi (1pi1091≤pi≤109, 1sin1≤si≤n) — the power and the endurance of the ii-th hero.

    It is guaranteed that the sum of n+mn+m over all test cases does not exceed 21052⋅105.

    Output

    For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or 1−1 if it is impossible).

    Example
    input
    Copy
    2
    6
    2 3 11 14 1 8
    2
    3 2
    100 1
    5
    3 5 100 2 3
    2
    30 5
    90 1
    
    output
    Copy
    5
    -1


    #include <bits/stdc++.h>
    using namespace std;
    void solve() {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        vector<int> mx(n);
        int m;
        cin >> m;
        for (int i = 0; i < m; ++i) {
            int x, y;
            cin >> x >> y;
            mx[y - 1] = max(mx[y - 1], x);//最多坚持y天的最大能力值
        }
        for (int i = n - 2; i >= 0; --i)//n只怪兽
            //得出可以坚持i天的英雄的最大能力值
            mx[i] = max(mx[i], mx[i + 1]);
        int ans = 0;
        for (int i = 0; i < n; ) {
            int j = i, x = 0;
            //从前端开始,
            //首先坚持1天的英雄的最大能力值,是否大于第一个怪兽的能力值
            //如果大于,j++,并取当前考虑过的怪兽的最大能力值,方便以后比较
            //然后看第二天,如果能坚持两天的英雄的最大能力值,大于前两个怪兽的最大能力值
            //接着往下考虑
            while (j < n && mx[j - i] >= max(x, a[j])) {
                x = max(x, a[j]);
                ++j;
            }
            //如果坚持一天的英雄的最大能力值,小于当前怪兽的能力,说明,
            //这个怪兽比所有英雄都nb,,那么直接输出-1
            if (j == i) {
                cout << -1 << "
    ";
                return;
            }、 
            ++ans;//加天数
            i = j;//上面考虑的最终结果
        }
        cout << ans << "
    ";
    }
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int T;
        cin >> T;
        while (T--)
            solve();
        return 0;
    }


  • 相关阅读:
    iOS开发的一些奇巧淫技2
    iOS开发的一些奇巧淫技
    指定控制器跳转
    去掉UITableView HeaderView或FooterView随tableView 移动的黏性
    TextView随键盘弹出上移高度
    The Swift Programming Language-官方教程精译Swift(9) 枚举-- --Enumerations
    登录记住账号和密码小Demo
    The Swift Programming Language-官方教程精译Swift(8)闭包 -- Closures
    The Swift Programming Language-官方教程精译Swift(7)函数 -- Functions
    The Swift Programming Language-官方教程精译Swift(6)控制流--Control Flow
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11863747.html
Copyright © 2020-2023  润新知