题意:给出n段木棍的首尾坐标,求出(两个坐标都)非降的木棍序列的最小个数。
排序后模拟即可。
代码:
/*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uvalive2322.cpp
* Lauguage: C/C++
* Create Date: 2013-09-06 20:50:20
* Descripton: greedy, sort
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mc(a) memset(a, 0, sizeof(a))
const int MAXN = 5001;
struct P {
int lhs, rhs;
} p[MAXN];
int t, n;
bool vis[MAXN];
bool cmp(P a, P b) {
if (a.lhs != b.lhs) return a.lhs < b.lhs;
else return a.rhs < b.rhs;
}
int main() {
scanf("%d", &t);
while (t--) {
// input
scanf("%d", &n);
rep(i, n) scanf("%d%d", &p[i].lhs, &p[i].rhs);
// sort
sort(p, p + n, cmp);
// solve
mc(vis);
int cnt = 0;
while (1) {
int i;
for (i = 0; i < n; i++)
if (!vis[i]) break;
if (i == n) break;
vis[i] = true;
int l = p[i].lhs, r = p[i].rhs;
for (i++; i < n; i++)
if (!vis[i] && p[i].lhs >= l && p[i].rhs >= r) {
vis[i]++;
l = p[i].lhs;
r = p[i].rhs;
}
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
分享到:
相关推荐
北大POJ2513-Colored Sticks【TrieTree+MergeSet+EulerPath】 解题报告+AC代码
贪心算法 WOODEN STICKS 实例代码,需要的朋友可以参考一下
Wooden Stickes 问题 现有n 根木棒,已知它们的长度和重量。要用一部木工机一根根加工,该机器在加工过程中需要一定的准备时间,是用于清洗机器在,调整工具和模板。 木工机需要的准备时间如下: (1)第1根需要1...
1025 Wooden Sticks 给出不同规格的木条给一定要求机器加工的最少时间 排序+贪心 1027 Human Gene Functions 求基因匹配度 DP:lcs的变形 1028 Flip and Shift 首尾相连的01串是否可经过以其中一位为中心的左右对调...
北大POJ1011-Sticks 解题报告+AC代码
zju 1025 Wooden Sticks http://acm.zju.edu.cn/show_problem.php?pid=1025
Wood Sticks
sticks cutting problem sticks are cut randomly until all parts became less than 50 units long. Now to return sticks to the original state. This program is designed to compute the smallest possible ...
国外的一款免安装版的桌面便签软件,支持定时提醒,方便大家管理纷繁复杂的工作事物
tech_shape_sticks
poj 2452 Sticks Problem.md
Very simple example code - sticks a sortkey in the buffer Not much error checking.
世界各国大学生优秀建筑毕业设计作品集
ice_sticks
bailian.openjudge.cn 1011 sticks
抽象精品ppt模板tech_shape_sticks056