zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    G1垃圾收集器入门-原创译文
    Dubbo微容器(Cooma)详解
    Dubbo工程结构和Maven打包
    Greys学习笔记(未完待续)
    ES学习笔记
    CSS标准盒子模型和IE怪异盒子模型
    npm install 报错
    安卓手机scrollTop失效问题记录
    npm run dev报错
    severless扫盲
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308865.html
Copyright © 2011-2022 走看看