$[Luogu]$ 洛谷 $P3434$ 题解【[POI2006]KRA-The Disks】

因为一些物理原因,盘子一定是向下掉的

所以我们可以搞一个数组r_min[i]来维护r[1]~r[i]的最小值

然后再弄一个指针指向r_min[n]

每次读入一个k,通过向上移动指针来确定每一个盘子放的位置

蒟蒻的代码 QAQ

#include<bits/stdc++.h>
using namespace std;
int n,m;
int r[300001],k,r_min[300001];
bool flo[300001];
inline int read()
{
    int x=0,w=0;char ch=0;
    while(!(ch>='0'&&ch<='9'))
    {
        if(ch=='-') w=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return w?-x:x;
}
int main(){
    n=read();
    m=read();
    for(register int i=1;i<=n;++i)
    {
        r[i]=read();
        if(i!=1) r_min[i]=min(r_min[i-1],r[i]);
        else r_min[i]=r[i];
    }
    int kkk=n;
    for(register int i=1;i<=m;++i)
    {
        k=read();
        int toto=kkk;
        while(k>r_min[kkk]) kkk--;
        if(kkk<=0){cout<<0; return 0;}
        if(toto==kkk&&i!=1) kkk--;
    }
    cout<<kkk;
}
原文地址:https://www.cnblogs.com/wo-shi-zhen-de-cai/p/9787493.html