• 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;
    }
    
  • 相关阅读:
    51Nod一级算法1002数塔取数问题
    素数筛法
    辗转相除法求最大公约数
    模型评估与选择
    Linux下的五种IO模型
    浮点类型丢失精度的问题
    Update操作浅析,一定是先Delete再Insert吗?
    SQLSERVER中返回修改后的数据
    MachineKey生成
    Katana的起源
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308865.html
Copyright © 2020-2023  润新知