Yet Another Monster Killing Problem
You play a computer game. In this game, you lead a party of m heroes, and you have to clear a dungeon with n monsters. Each monster is characterized by its power ai. Each hero is characterized by his power pi and endurance si.
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 k monsters, the hero fights with the monster k+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 i-th hero cannot defeat more than si monsters 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 t (1≤t≤105) — the number of test cases. Then the test cases follow.
The first line of each test case contains one integer n (1≤n≤2⋅105) — the number of monsters in the dungeon.
The second line contains n integers a1, a2, …, an (1≤ai≤109), where ai is the power of the i-th monster.
The third line contains one integer m (1≤m≤2⋅105) — the number of heroes in your party.
Then m lines follow, each describing a hero. Each line contains two integers pi and si (1≤pi≤109, 1≤si≤n) — the power and the endurance of the i-th hero.
It is guaranteed that the sum of n+m over all test cases does not exceed 2⋅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 if it is impossible).
Example
input
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
5
-1
题意:
输入一个数n表示有n个怪物,接下来一行n个数,第i个数表示第i个怪物的力量,然后一个数m表示你有的英雄数,接下来m行是你的英雄,每行有两个数 p,s分别表示这个英雄的力量和耐力。你需要进行打怪,每天你只能派一名英雄去带怪,这个英雄必须按照怪物的顺序打,如果英雄的力量大于怪物的力量就可以打败怪物,同时消耗1点耐力,如果这个英雄耐力用完这个英雄当天就不能进行打怪了,题目问你最少多少天可以将怪物打完,如果打不完的话就输出-1,否则输出最少天数
思路:
首先能用到贪心的思想,如果两个英雄耐力相等的话肯定选那个力量大的,同样的如果力量相等的话肯定选那个耐力多的,根据题目给你的范围,你可以通过耐力选力量更大的,这是第一步。第二步的话就是进行类似dp的更新,因为这个题的范围,你只能进行一层for循环,要不然会超限,所以你可以这样先从耐力最大的往下更新,把他们的力量更新为前面最大的,然后按照顺序进行打怪然后更新天数,直到打完为止
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int inf=0x3f3f3f3f;
int p[200005];
int v[200005];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int a=1;a<=n;a++)
{
scanf("%d",&v[a]);
p[a]=0;
}
int m,x,y;
scanf("%d",&m);
for(int a=1;a<=m;a++)
{
scanf("%d%d",&x,&y);
p[y]=max(p[y],x);
}
for(int a=n-1;a>=1;a--)
{
p[a]=max(p[a],p[a+1]); //按照耐力从大到小更新力量值
}
int mx=0,num=1,flog=0,ans=0;
for(int a=1;a<=n;a++)
{
mx=max(mx,v[a]);
if(p[a-ans]<mx)
{
mx=v[a];
num++;
ans=a-1;
}
if(p[1]<mx)
{
flog=1;
break;
}
}
if(flog)
{
printf("-1\n");
}
else
{
printf("%d\n",num);
}
}
return 0;
}