超时代码“
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
struct node
{
int x;
int y;
}stick[5005];
bool f[5005];
bool cmp(node a, node b)
{
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
int main()
{
int T;
cin >> T;
while (T--)
{
node temp[5005];
memset(f, true, sizeof(f));
int i, j;
int n;
cin >> n;
for (i = 1; i <= n; i++)
{
scanf("%d%d",&stick[i].x ,&stick[i].y);
}
sort(stick + 1, stick + n + 1, cmp);
int count = 0;
int Sum = 0;
int temp1 = n;
while (count != n)
{
int k = 1;
int num = 0;
for (i = 1; i <= temp1; i++)
{
if (f[i] == true)
{
temp[k++].y = stick[i].y;
num++;
}
}
for (i = 1; i <= num; i++)
{
if (temp[i].y >=temp[i - 1].y)
{
f[i] = false;
count++;
}
}
Sum++;
temp1 = num;
}
cout << Sum << endl;
}
return 0;
}
正确代码
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
struct node
{
int x;
int y;
int flag;
}stick[5005];
bool cmp(node a, node b)
{
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
int main()
{
int T;
cin >> T;
while (T--)
{
memset(stick, 0, sizeof(stick));
int i, j;
int n;
cin >> n;
for (i = 1; i <= n; i++)
{
scanf("%d%d",&stick[i].x ,&stick[i].y);
}
sort(stick + 1, stick + n + 1, cmp);
int count = 0;
int Sum = 0;
for (i = 1; i<=n; i++)
{
if (stick[i].flag == 0)
{
int temp;
count++;
temp = i;
for (j = i + 1; j<=n; j++)
{
if (stick[j].flag == 0 && stick[temp].y <= stick[j].y)
{
temp = j;
stick[j].flag = 1;
}
}
}
}
printf("%d\n", count);
}
return 0;
}