D. Yet Another Monster Killing Problem
You play a computer game. In this game, you lead a party of 𝑚 heroes, and you have to clear a dungeon with 𝑛 monsters. Each monster is characterized by its power 𝑎𝑖. Each hero is characterized by his power 𝑝𝑖 and endurance 𝑠𝑖.
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 𝑘 monsters, the hero fights with the monster 𝑘+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 𝑖-th hero cannot defeat more than 𝑠𝑖 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 𝑡 (1≤𝑡≤105) — the number of test cases. Then the test cases follow.
The first line of each test case contains one integer 𝑛 (1≤𝑛≤2⋅105) — the number of monsters in the dungeon.
The second line contains 𝑛 integers 𝑎1, 𝑎2, ..., 𝑎𝑛 (1≤𝑎𝑖≤109), where 𝑎𝑖 is the power of the 𝑖-th monster.
The third line contains one integer 𝑚 (1≤𝑚≤2⋅105) — the number of heroes in your party.
Then 𝑚 lines follow, each describing a hero. Each line contains two integers 𝑝𝑖 and 𝑠𝑖 (1≤𝑝𝑖≤109, 1≤𝑠𝑖≤𝑛) — the power and the endurance of the 𝑖-th hero.
It is guaranteed that the sum of 𝑛+𝑚 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個怪獸 ,每個怪獸都有能力值a[i]。
然后現在你有m個英雄,每個英雄也有能力值p[i],每個英雄還有一個s[i],表示這個英雄一天最多能消滅多少個怪獸
現在你必須一個接一個的消滅怪獸,不能改變順序,然后問你最少多少天,能夠消滅所有的怪獸
題解
我們維護一個數組d[i],表示至少堅持i天的英雄的能力最大是多少。
然后我們對於每一天,進行貪心選擇最多的即可。我們優先看堅持一天的,能否大於最大的,然后看堅持兩天的,這樣一直看下去即可。
代碼
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int power[maxn],en[maxn],mons[maxn],hero[maxn];
void solve(){
int n,mxm=-1,mxh=-1,m,ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&mons[i]);
mxm=max(mxm,mons[i]);
hero[i]=0;
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d%d",&power[i],&en[i]);
mxh=max(mxh,power[i]);
hero[en[i]-1]=max(hero[en[i]-1],power[i]);
}
if(mxh<mxm){
puts("-1");
return;
}
for(int i=n-2;i>=0;i--){
hero[i]=max(hero[i],hero[i+1]);
}
int day=0,cur=-1,cons=-1;
while(day<n){
cur=max(cur,mons[day]);
cons++;
if(hero[cons]<cur){
ans++;
cons=0;
cur=mons[day];
}
day++;
}
ans++;
cout<<ans<<endl;
}
int main(){
int t;scanf("%d",&t);
while(t--){
solve();
}
return 0;
}