• 题解 CF1575L Longest Array Deconstruction


    CF1575L Longest Array Deconstruction / 原题链接

    其实这道题不用高级数据结构 2333

    题意

    如果将 \(a_i\) 移动到 \(i\),那么需要 \(a_i\le i\)

    考虑两个相邻的位置 \(p,q\),如果想让它们都有让答案加一的可能,要有:

    \[a_p<a_q\\p-a_p\le q-a_q \]

    而且发现这两个不等式同时也保证了 \(p<q\)

    对于不相邻的 \(p,q\),会依次考虑 \(p,p+1,p+2...q-1,q\),从而保证不会出错。

    于是就相当于求 \((i-a_i,a_i)\) 的 LIS。用 lower_bound 计算即可。

    时间复杂度 \(O(nlogn)\)

    代码

    #include<bits/stdc++.h>
    #define rep(i,x,y) for(int i=x;i<=y;++i)
    using namespace std;
    const int n7=201234;
    struct dino{int x,y;}a[n7];
    int n,b[n7],cnt,poi;
    
    int rd(){
    	int shu=0;bool fu=0;char ch=getchar();
    	while( !isdigit(ch) )fu|=(ch=='-'),ch=getchar();
    	while( isdigit(ch) )shu=shu*10+ch-'0',ch=getchar();
    	return fu?-shu:shu;
    }
    
    bool cmp(dino p,dino q){return p.x==q.x?p.y<q.y:p.x<q.x;}
    
    int main(){
    	n=rd();
    	rep(i,1,n){
    		int w=rd();
    		if(i-w>=0)cnt++,a[cnt]=(dino){i-w,w};
    	}
    	if(!cnt){puts("0");return 0;}
    	sort(a+1,a+cnt+1,cmp);
    	poi=1,b[1]=a[1].y;
    	rep(i,2,cnt){
    		if(a[i].y>b[poi])poi++,b[poi]=a[i].y;
    		else b[lower_bound(b+1,b+poi+1,a[i].y)-b]=a[i].y;
    	}
    	printf("%d",poi);
    	return 0;
    }
    
    
  • 相关阅读:
    BZOJ 4886 Lydsy1705月赛 叠塔游戏
    BZOJ 4552 TJOI2016&&HEOI2016 排序
    BZOJ 3702 二叉树
    BZOJ 4756 Usaco2017 Jan Promotion Counting
    BZOJ 4842 Neerc2016 Delight for a Cat
    BZOJ 1283 序列
    BZOJ 4819 SDOI2017 新生舞会
    BZOJ 1531 POI2005 Bank notes
    BZOJ 1925 SDOI2010 地精部落
    BZOJ WC2006 水管局长数据加强版
  • 原文地址:https://www.cnblogs.com/BlankAo/p/15826652.html
Copyright © 2020-2023  润新知