/*
思路:首先对木棒排序,按length升序排序,length相同,则按weight升序排序
然后在weight中通过贪心法找有多少个递增序列
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_NUM = 5002;
struct stick{
int len, wgt;
}stk[MAX_NUM];
bool vis[MAX_NUM];
bool cmp(const stick& stk1, const stick& stk2)
{
if (stk1.len < stk2.len)
return true;
else if (stk1.len == stk2.len)
return stk1.wgt < stk2.wgt;
else return false;
}
int main()
{
int c, n, i, j, cnt, wgt;
scanf("%d", &c);
while (c--){
scanf("%d", &n);
cnt = 0;
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; ++i)
scanf("%d%d", &stk[i].len, &stk[i].wgt);
sort(stk, stk+n, cmp);
for (i = 0; i < n; ++i){
if (!vis[i]){
vis[i] = true, cnt++;
wgt = stk[i].wgt;
for (j = i+1; j < n; ++j){
if (!vis[j] && stk[j].wgt >= wgt)
vis[j] = true, wgt = stk[j].wgt; //记录当前序列最大值
}
}
}
printf("%d\n", cnt);
}
return 0;
}
POJ 1065 (贪心法)
最新推荐文章于 2020-05-13 10:58:19 发布