跳到正文

题解

题解 P1233 【木棍加工】

qseer约 1 分钟
原文
文章信息与版本

文章信息

发布
2018年10月18日
收录
2026年2月7日
更新
2026年11月18日
篇幅
约 1 分钟
题目
P1233

版本

  1. v2当前
  2. v1

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

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

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

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

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

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

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

code

C
#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
*/
原文
存档评论1 条抓取于