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).
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1
5
-1
先附上我的错误思路,错误代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<math.h>
using namespace std;
typedef long long ll;
struct node
{
int p,s;
}str[200005];
bool cmp(node a,node b)
{
return a.s>b.s;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,a[200005],max1=0,max2=0,day=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
max1=max(max1,a[i]);
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d %d",&str[i].p,&str[i].s);
max2=max(max2,str[i].p);
}
sort(str,str+m,cmp);
if(max1>max2)
{
printf("-1\n");
continue;
}
int k=0,x=0;
for(int i=0;i<n;i++)
{
if(a[i]>str[k].p||x==str[k].s)
{
day++;
for(k=0;k<m;k++)
{
if(str[k].p>=a[i])
{
x=1;
break;
}
}
}
else x++;
}
printf("%d\n",day+1);
}
return 0;
}
上面只注意到了天数和s[i]有关,而没有注意到他的权重,总之考虑不周全。
后来找到了大佬的代码(我终于醒悟了),然后一发ac。。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<math.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,a[200005],max1=0,max2=0,cost[200005],day=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
max1=max(max1,a[i]);
cost[i]=0;
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int p,s;
scanf("%d %d",&p,&s);
max2=max(max2,p);
cost[s-1]=max(cost[s-1],p);
}
if(max1>max2)
{
printf("-1\n");
continue;
}
for(int i=n-2;i>=0;i--) cost[i]=max(cost[i+1],cost[i]);
int x=0,y=0;
for(int i=0;i<n;i++)
{
x=max(x,a[i]);
if(cost[y]<x)
{
day++;
y=0;
x=a[i];
}
y++;
}
printf("%d\n",day+1);
}
return 0;
}