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 (1≤t≤1051≤t≤105) — the number of test cases. Then the test cases follow.

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

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

The third line contains one integer mm (1≤m≤2⋅1051≤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 (1≤pi≤1091≤pi≤109, 1≤si≤n1≤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 2⋅1052⋅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 = ; i < n; ++i)
cin >> a[i];
vector<int> mx(n);
int m;
cin >> m;
for (int i = ; i < m; ++i) {
int x, y;
cin >> x >> y;
mx[y - ] = max(mx[y - ], x);//最多坚持y天的最大能力值
}
for (int i = n - ; i >= ; --i)//n只怪兽
//得出可以坚持i天的英雄的最大能力值
mx[i] = max(mx[i], mx[i + ]);
int ans = ;
for (int i = ; i < n; ) {
int j = i, x = ;
//从前端开始,
//首先坚持1天的英雄的最大能力值,是否大于第一个怪兽的能力值
//如果大于,j++,并取当前考虑过的怪兽的最大能力值,方便以后比较
//然后看第二天,如果能坚持两天的英雄的最大能力值,大于前两个怪兽的最大能力值
//接着往下考虑
while (j < n && mx[j - i] >= max(x, a[j])) {
x = max(x, a[j]);
++j;
}
//如果坚持一天的英雄的最大能力值,小于当前怪兽的能力,说明,
//这个怪兽比所有英雄都nb,,那么直接输出-1
if (j == i) {
cout << - << "\n";
return;
}、
++ans;//加天数
i = j;//上面考虑的最终结果
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return ;
}

Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem的更多相关文章

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

    D. Yet Another Monster Killing Problem You play a computer game. In this game, you lead a party of

  2. Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest

    Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定3个人互不相同的多个数字,可以 ...

  3. Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)

    题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...

  4. Educational Codeforces Round 76 (Rated for Div. 2)

    传送门 A. Two Rival Students 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/13 22:37:26 */ #incl ...

  5. Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest dp

    E. The Contest A team of three programmers is going to play a contest. The contest consists of

  6. Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题

    C. Dominated Subarray Let's call an array

  7. Educational Codeforces Round 76 (Rated for Div. 2) B. Magic Stick 水题

    B. Magic Stick Recently Petya walked in the forest and found a magic stick. Since Petya really likes ...

  8. Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题

    A. Two Rival Students There are

  9. Educational Codeforces Round 76 (Rated for Div. 2) D题

    题意: 给你n个关卡,每个关卡有一个怪物,怪物的攻击力为a[i],你有n个英雄,每个英雄有一个攻击力,和疲劳值,只要英雄的攻击力比怪物的高就算打过了,同时疲劳减一,一天只能出战一个英雄,一个英雄可以打 ...

随机推荐

  1. Django中非视图函数获取用户对象

    今天遇到了一个问题:在Django中怎么从非视图函数中获取用户对象?怎么保证不同的请求获取到不同的用户对象? 平常我们获取用户对象使用的是: request.user 不得不说,这确实很方便. 但是, ...

  2. Spring boot项目的打包发布

    Eclipse打包发布项目 打包项目 首先需要将项目编译的文件删除,执行[Run As]->[Maven clean] 如果这个时候项目报错,在pom.xml文件中添加以下代码过滤掉单元测试 & ...

  3. Open Live Writer(olw)博客写作软件

    前言 wlw似乎不再提供下载了,从微软的官网下载安装程序之后,无法联网下载olw组件,所以写博客改用olw. olw是wlw的开源版本,所以wlw上的操作是可以在olw上继续使用的. 关于wlw的知识 ...

  4. yamlapi接口测试框架

    1.思路: yamlapi支持unittest与pytest两种运行模式, yamlapi即为yaml文件+api测试的缩写, 可以看作是一个脚手架工具, 可以快速生成项目的各个目录与文件, 只需维护 ...

  5. Python三元表达式、列表推导式、生成器表达式

    1. 三元表达式 name=input('姓名>>: ') res='SB' if name == 'aaaa' else 'NB' print(res) 2. 列表推导式 #1.示例 e ...

  6. Linux网络课程学习第一天

    第一天上课主要介绍了LINUX系统和Linux课程的情况.了解了开源系统的四大优势,六大特点. 最具有心得的一句话: 学习是件苦差事: 稻盛和夫先生的话也深深激励着我:“工作马马虎虎,只想在兴趣和游戏 ...

  7. Spring学习笔记-装配Bean-02

    什么是装配 创建应用对象之间写作关系的行为通常称为装配(wiring),这也是依赖注入(DI)的本质. Spring配置的可选方案 Spring提供了3中主要的装配机制: ● 在XML中进行显式配置. ...

  8. json 的key值不能是变量

    var _key = name; var _value = 2; var params = { _key :_ value } _key 为变量 console.log(params); { _key ...

  9. java注释英语自动机翻

    一.简介 每次面试都被怼spring源码你看过吗?你用spring开发源码你都不看的吗?这样怎么开发?那我就下点决心,趁着现在疫情在家宅,我要看spring源代码.但是发现注释都是英文,勉勉强强能看懂 ...

  10. BZOJ-2424: [HAOI2010]订货【费用流】

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1487  Solved: 1002[Submit][Status][Discuss] Descript ...