洛谷p1233 sort+贪心

题目描述

一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的。棍子可以被一台机器一个接一个地加工。机器处理一根棍子之前需要准备时间。准备时间是这样定义的:

第一根棍子的准备时间为1分钟;

如果刚处理完长度为L,宽度为W的棍子,那么如果下一个棍子长度为Li,宽度为Wi,并且满足L>=Li,W>=Wi,这个棍子就不需要准备时间,否则需要1分钟的准备时间;

计算处理完n根棍子所需要的最短准备时间。比如,你有5根棍子,长度和宽度分别为(4, 9),(5, 2),(2, 1),(3, 5),(1, 4),最短准备时间为2(按(4, 9)、(3, 5)、(1, 4)、(5, 2)、(2, 1)的次序进行加工)。
输入格式

第一行是一个整数n(n<=5000),第2行是2n个整数,分别是L1,W1,L2,w2,…,Ln,Wn。L和W的值均不超过10000,相邻两数之间用空格分开。
输出格式

仅一行,一个整数,所需要的最短准备时间。

其实看数据n的范围,n^2是可以接受的(瞎搞都可以!!!);
那么其实每一根棍都想找一个可以让他不用准备时间的棍子作为上一根棍子。
我们对长和宽排序,宽的大在前,一样大的长在前(反过来也可以)。我们就从上往下去枚举就可以了,因为我们已经使长的最大的在前面了,我们就只要从上往下找看看有什么是符合条件的木棍,那根棍就可以直接拿(拿完后上一根的数值就要变为这一根了,也就是长宽的限制条件要改变)。枚举每一根木棍就直接可以有答案了。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
const int max_ = 1e7 + 7;
int n, head[max_], xiann = 1, ml,md;
inline int read()
{
	int s = 0, f = 1;
	char ch = getchar();
	while (ch<'0' || ch>'9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0'&&ch <= '9') {
		s = s * 10 + ch - '0';
		ch = getchar();
	}
	return s * f;
}
struct k {
	int chang, kuang,pan;
}node[max_];
bool cmp(const k & t1, const k &t2) {
	if (t1.chang == t2.chang) {
		return t1.kuang > t2.kuang;
	}
	return t1.chang > t2.chang;
}
int main() {
	n = read();
	for (int i = 1; i <= n; i++) {
		node[i].chang = read();
		node[i].kuang = read();
		node[i].pan = 0;
	}
	sort(node + 1, node + 1 + n, cmp);
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (node[i].pan == 1)continue;
		ans++;
		int standc = node[i].chang, standk = node[i].kuang;
		for (int j = i + 1; j <= n; j++) {
			if (node[j].pan == 1)continue;
			if (standc >= node[j].chang &&standk >= node[j].kuang) {
				standc = node[j].chang;
				standk = node[j].kuang;
				node[j].pan = 1;
			}
		}
	}
	cout << ans;

	return 0;
}

发布了32 篇原创文章 · 获赞 3 · 访问量 675

猜你喜欢

转载自blog.csdn.net/qq_43804974/article/details/101012951
今日推荐