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;
    }
    
  • 相关阅读:
    1. 马尔科夫决策过程
    梯度下降法、随机梯度下降法、小批量梯度下降法
    计算曲线与坐标轴的面积
    鼠标进入,显示div
    codewar
    Django firstDay
    C#学习之关于lock
    winfrom 界面中表格数据修改及刷新(DEV)
    SVN 分支合并 版本控制
    wpf 绑定非元素对象
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308864.html
Copyright © 2011-2022 走看看