题目大意:一个机器对n个木棒进行加工,输入n个木棒的长度和重量,
如果符合条件length1≤length2同时weight1≤weight2,则机器不需要安装时间。
否则,需要等待1分钟。求最少需要准备几分钟。
分析:对机器进行二次排序(先length然后weight),第一次寻找尽可能多的符合条件的木棒
并用mark[i]=1,第二次在mark[i]=0中寻找尽可能多的符合条件的木棒并用mark[i]=1,直到
所有mark[i]=1。
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<math.h>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<algorithm>
#include<iostream>
using namespace std;
typedef struct wood{
int length;
int weight;
};
bool cmp(wood a, wood b) {
if (a.length < b.length)
return true;
else if (a.length > b.length)
return false;
else
return a.weight < b.weight;
}
int main() {
int t, n;
wood w[5010];
int mark[5010];
cin >> t;
while (t--) {
cin >> n;
memset(mark, 0, sizeof(mark));//标记
for (int i = 0; i < n; i++) {
cin >> w[i].length >> w[i].weight;
}
sort(w, w + n, cmp);
int number = 0;
int cnt = 0;
int flag;
while (number != n) {
for (int i = 0;i < n;i++)
{
if (!mark[i]) {
flag = i;
cnt++;
break;
}
}
for (int i = 0;i < n;i++)
{
if (!mark[i] && w[i].length >= w[flag].length && w[i].weight >= w[flag].weight) {
mark[i] = 1;
number++;
flag = i;
}
}
}
cout << cnt << endl;
}
system("pause");
return 0;
}