Learn a new posture --- suffix to modify the array after stupid questions in div2 of D usually do not get stuck, this problem is relatively simple D, read explanations, greedy new posture! !
This question is intended title: monster with n, ai is the power of each monster
There m warriors, power is pi, si is the degree of tolerance
When the Warriors monster power than the current degree of tolerance is zero or small end of the day, or to continue. Find the minimum number of days.
Solution: This problem required minimum number of days that you can try with the greedy think the problem because each hero can be used many times, we certainly take into account the large and great patience to use the power of the warriors to killing
Accordingly array disposed bst, bsti represents endurance greater than or equal warrior day i power is the largest number (the suffix modified !!!). This will enumerate the monster, with the growth of the number of days of observation when to stop
We set the coordinates of the current did not kill the monster is npos, cnt initial value of 0 each, representing a degree of tolerance to the killing of cnt is greater than the hero (obviously a large number of days hero capacity value <= small number of days, because one day play not necessarily play for two days, two days certainly play play one day).
If this monster be killed, then npos ++, has been the case so far can not be killed, noting that, if greater than or equal day's hero can not kill, then no solution.
#include<iostream> #include<cstring> #include<vector> #include<cmath> #include<string> #define x first #define y second using namespace std; typedef long long ll; const int N=2e5+10; int bst[N]; int a[N]; int n,m; int s[N]; int p[N]; int main(){ int t; scanf("%d",&t); while(t--){scanf("%d",&n); for(int i = 0; i <= n; ++i) bst[i] = 0; int i; for(i=1;i<=n;i++){ scanf("%d",&a[i]); } scanf("%d",&m); for(i=1;i<=m;i++){ scanf("%d%d",&p[i],&s[i]); bst[s[i]]=max(bst[s[i]],p[i]); } for(i=n-1;i>=1;i--){ bst[i]=max(bst[i],bst[i+1]); } int res=0; int pos=1; bool ok=true; while(pos<=n){ res++; int npos=pos; int mx=0; while(true){ mx=max(mx,a[npos]); if(mx>bst[npos-pos+1]){ break; } npos++; } if(npos==pos){ ok=false; break; } pos=npos; } if(ok==false) printf("-1\n"); else printf("%d\n",res); } }