zoukankan      html  css  js  c++  java
  • 题解 P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird

    首先,这道题的暴力dp非常好写
    就是枚举所有能转移到他的点,如果当前枚举到的位置的值大于
    当前位置的话,(f[i]=min(f[i],f[j]))
    否则就(f[i]=min(f[i],f[j]+1))
    时间复杂度为O(nk)
    考虑优化,因为只是加1或不加,实际上只需要知道能转移到当前节点的位置
    的&f&数组最小值即可,同时也要选这个位置数值最大的(想一想为什么)
    这样的话,这道题就没了

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int N=1e6+7;
    int n,k,Q;
    int f[N],a[N];
    int q[N],h1,h2;
    int main(){
    //	freopen("t4.in","r",stdin);
    //	freopen("t4.out","w",stdout);
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    //	memset(f,0x3f,sizeof(f));
    	f[1]=0;
    	scanf("%d",&Q);
    	while(Q--){
    		scanf("%d",&k);
    		h1=h2=1;
    		q[h1]=1;
    		for(int i=2;i<=n;i++){
    			while(h1<=h2&&i-q[h1]>k) h1++;
    			if(a[i]>=a[q[h1]]) f[i]=f[q[h1]]+1;
    			else f[i]=f[q[h1]];
    			while(h1<=h2&&(f[i]<f[q[h2]]||(f[i]==f[q[h2]]&&a[i]>=a[q[h2]]))) h2--;
    			q[++h2]=i;
    		}
    		cout<<f[n]<<"
    ";
    	}
    	return 0;
    }
    /*
    9
    4 6 3 6 3 7 2 6 5 
    2
    2
    5
    */
    
  • 相关阅读:
    如何在Altium中下载并添加软件没有的苦文件【转】
    20121124
    变量作用域&函数作用域
    http相关知识
    函数声明和函数表达式
    js中constructor和prototype
    委托模式
    js跨域
    原型和原型链
    javascript 数据类型
  • 原文地址:https://www.cnblogs.com/Aswert/p/13658683.html
Copyright © 2011-2022 走看看