题目链接点击打开链接
第一个木头需要1min的启动时间,
其余的木头如果满足 l<=l' and w<=w'则不需要启动时间
贪心,首先按照(1)n1.l < n2.l
(2)n1.w < n2.w
进行排序
然后贪心的去找每一个满足 l<=l' and w<=w'的序列,用vis[i]标记已经找过的
如{1,2},{1,3},{2,2},{3,5},{5,3}
第一遍找出{1,2},{1,3},{3,5}
第二遍找出{2,2},{3,5}
#include <stdio.h>
#include <algorithm>
#include <memory.h>
using namespace std;
const int maxn = 5000 + 5;
struct node{
int l, w;
}sticks[maxn];
bool vis[maxn];
bool cmp(const node &n1, const node &n2)
{
if (n1.l < n2.l)return true;
else if (n1.l == n2.l){
return n1.w < n2.w;
}
return false;
}
int main()
{
//freopen("in.txt", "r", stdin);
int T, n, ans;
scanf("%d", &T);
while (T--){
ans = 0;
memset(vis, 0, sizeof(vis));
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d %d", &sticks[i].l, &sticks[i].w);
}
sort(sticks, sticks + n, cmp);
for (int i = 0; i < n; ++i){
if (!vis[i]){
int weight = sticks[i].w;
for (int j = i + 1; j < n; ++j){
if (!vis[j] && weight <= sticks[j].w) {
vis[j] = true;
weight = sticks[j].w;
}
}
ans++;
}
}
printf("%d\n", ans);
}
return 0;
}