Codeforces1257 D - Yet Another Monster Killing Problem 贪心 区间延续

b数组卡memset超时
scanf 比IO cin cout 快4倍
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=6e5+5;
const ll mod=1e9+7;
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pb push_back
ll n,m,ans,cnt;
ll a[maxn],b[maxn],minn=2e9;
ll T,x,y,res;
bool flag;


int main(){

	scanf("%lld",&T);
	while(T--){
		res=0;
		flag=true;
		for(int i=1;i<=n;i++)
			b[i]=0;//忍耐度为1-n对应的能量值 
		scanf("%lld",&n);
		for(int i=1;i<=n;i++){
			scanf("%lld",a+i);
		}
		scanf("%lld",&m);
		while(m--){
			scanf("%lld %lld",&x,&y);
			b[y]=max(x,b[y]);
		}
		for(int i=n;i>=1;i--){
			b[i]=max(b[i],b[i+1]);//相同忍耐度的最大能量值 并且延续上一个忍耐度来的最大能力值 
		}
		ll pos=1,npos=1,mx=0;
		while(pos<=n){
			res++;//第几天 
			npos=pos;
			mx=0;
			while(true){
				mx=max(mx,a[npos]);
				if(mx>b[npos-pos+1])	break;//n个忍耐度的最大能力值都大 
				npos++;
			}
			if(pos==npos)
			{
				flag=false;
				break;
			}
			pos=npos;
		}
		if(!flag)
			printf("-1\n");
		else
			printf("%lld\n",res);
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值