http://acm.pku.edu.cn/JudgeOnline/problem?id=1065
刚开始错了,下面是出错的代码,弄清楚原因下次小心点;
#include <iostream>
#include <algorithm>
using namespace std;
struct TanXin
{
int width, length;
}wood[5001], record[5001];
bool order(TanXin a, TanXin b)
{
return (b.width + b.length > a.width + a.length);
}
int main()
{
int i, j;
int n;
int t;
int test;
int l;
int k;
cin >> test;
while(test--)
{
cin >> n;
for(i = 0; i < n; i++)
cin >> wood[i].length >> wood[i].width;
sort(wood, wood + n, order);
for(i = 0; i < n; i++)
// cout << wood[i].length << " " << wood[i].width << " " << wood[i].length + wood[i].width << endl;
record[0] = wood[0];
l = 1;
for(i = 1; i < n; i++)
{ t = 1;
for(j = 0; j < l; j++)
{
//以下的算法错误,因为每次都从较小的record[0]开始覆盖,会产生错误的
//例如: 4 ,3 5, 8 4, 8 5, 3 100;8,5先覆盖3,5,所以3, 100 覆盖不了3, 5正确答案为2,而此时为3;
if(wood[i].width >= record[j].width && wood[i].length >= record[j].length)
{
if(wood[i].width > record[j].width)
record[j].width = wood[i].width;
if(wood[i].length > record[j].length)
record[j].length = wood[i].length;
t = 0;
break;
}
}
if(t == 1)
{
record[l] = wood[i];
l++;
}
// cout << "record: " << l << endl;
// for(k = 0; k < l; k++)
// cout << record[k].width << " " << record[k].length << endl;
}
cout << l << endl;
}
return 0;
}