题解 P1233 【木棍加工】

· · 题解

为什么我的眼里只有单调栈?

我们用结构体存储木棍的长和宽,先以长度排序(降序)

我们发现,如果宽度也呈单调递减(好像是不严格单调),那就可以以一次的花费切完这些木棍

这就相当于用了几个栈(也就是栈的数量)

如果不单调,也很简单,再加一个栈,这是必须的,别无选择

从后面加进来的 bi 依次遍历已有的栈,如果没有,就添加,ans++

于是答案就转化为了统计栈的数量

code

#include<stdio.h> 
#include<algorithm> 
using namespace std;
struct node {
    int a,b;
}se[5100];
int n,cnt,stk[5100][5100],top[5100];

bool operator < (const node &x,const node &y) {
    return x.a>y.a;
}

int main() 
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i) 
        scanf("%d%d",&se[i].a,&se[i].b);
    sort(se+1,se+1+n);
    for(int i=1;i<=n;++i) {
        int d=1;
        while(top[d] && stk[d][top[d]]<se[i].b) ++d;
        stk[d][++top[d]]=se[i].b;
        cnt=max(cnt,d);
    }
    printf("%d",cnt);
    return 0;
}
/*
5
4 9 
5 2 
2 1 
3 5 
1 4
*/