Codeforces 1257D Yet Another Monster Killing Problem

思路:

这题又是参考的cf自带题解:

At first, lets precalc array bst; bsti is equal to maximum hero power whose endurance is greater than or equal to i.
Now let’s notice that every day it’s profitable for as to kill as many monster as possible. Remains to understand how to calculate it.
Suppose that we already killed cnt monsters. If acnt+1>bst1 then answer is -1, because we can’t kill the cnt+1-th monster. Otherwise we can kill at least x=1 monsters. All we have to do it increase the value x until conditions maxcnt<i≤cnt+xai≤bstx holds.
After calculating the value x we just move to the next day with cnt+x killed monsters.

整体思路就是贪心,每次看能不能往前打一个怪;
这题领会了一个坑点,用给memset给数组赋初值为0是真的真的慢,一直被卡超时 = =


代码:

#include<bits/stdc++.h>
using namespace std;
#define rp(i,n) for(int i=0;i<n;i++)
#define rpn(i,n) for(int i=1;i<=n;i++)
const int MAX_N=200005;
int arr[MAX_N];//大于等于i的endurance里power的最大值 
int mst[MAX_N];
int main(){
	int t;
	scanf("%d",&t);
	rp(i,t){
		int n,m;
		scanf("%d",&n);
		for(int j=0;j<=n;j++) arr[j]=mst[j]=0;
		rpn(j,n) scanf("%d",mst+j);
		scanf("%d",&m);
		rpn(j,m){
			int p,s;
			scanf("%d%d",&p,&s);
			arr[s]=max(arr[s],p);
		}
		for(int j=n-1;j>=0;j--) arr[j]=max(arr[j+1],arr[j]);
		int pos=1,cnt=0;//第pos个怪物 
		while(pos<=n){
			cnt++;
			int ans=0,maxp=mst[pos];//今天可以向前推进ans个怪物 
			while(maxp<=arr[ans+1]&&pos<=n){//如果可以向前推进 
				ans++;pos++;
				maxp=max(maxp,mst[pos]);
			} 
			if(ans==0) break;
		}
		printf("%d
",pos<=n?-1:cnt);
	} 
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308864.html