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;
    }
    
  • 相关阅读:
    ahjesus动态生成表达式树
    ahjesus sql2005+游标示例
    Ahjesus获取自定义属性Attribute或属性的名称
    快速设置超炫banner,js插件
    清除浮动after
    Javascript中void操作符
    ahjesus js 快速求幂
    "静态控件"组件:<static> —— 快应用组件库H-UI
    "搜索栏"组件:<search> —— 快应用组件库H-UI
    "穿梭框"组件:<transfer> —— 快应用组件库H-UI
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308865.html
Copyright © 2011-2022 走看看