zoj 3197解决方案
zoj 3197
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3197
一直报错,受不了了。
帮忙看下算法哪里错了
------解决方案--------------------
5
1 3
2 4
3 5
4 4
5 5
修正一个不合法数据。反正意思你懂的。
------解决方案--------------------
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3197
一直报错,受不了了。
帮忙看下算法哪里错了
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int N = 5010;
typedef struct Node
{
int x,y;
};
Node p[N];
bool comp(Node s1, Node s2)
{
if(s1.x != s2.x)
return s1.x < s2.x;
return s1.y < s2.y;
}
int main()
{
int T;
int n;
cin>>T;
int from, to, cnt;
while(T--)
{
cin>>n;
for(int i=0; i<n; i++)
{
cin>>p[i].x>>p[i].y;
}
sort(p, p +n,comp);
int cnt=0;
int end=0;
if(n==1) cnt=1;//如果只有一个元素
for(int i=1;i<n;i++)
{
if(p[i].x!=p[i-1].x && end<p[i-1].y) //选x下表最大的
{
end=p[i-1].y;
cnt++;
if(end==n) break;
}
if(i==n-1)//如果前n-1个,end都小于n,最后一个元素肯定到n
{
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}
------解决方案--------------------
5
1 3
2 4
3 5
4 4
5 5
修正一个不合法数据。反正意思你懂的。
------解决方案--------------------