题目
两种做法,开始用贪心做的,有种情况没考虑到,结果排序错了。
这个例子,感觉上有三个交点,以为是30,其实是20.
贪心代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct node{int x,y;
};
node no[250];
int flag[250];
int cmp(node a,node b)
{if(a.x<b.x)return 1;else if(a.x==b.x)return a.y<b.y;else return 0;
}
int main()
{int n,a,b;//freopen("input.txt","r",stdin);int T;cin>>T;while(T--){cin>>n;for(int i=0;i<n;i++){cin>>a>>b;if(a%2==1) a++;if(b%2==1) b++;if(a>b){int t = a;a = b;b = t;}no[i].x=a,no[i].y=b;}sort(no,no+n,cmp);memset(flag,0,sizeof(flag));int cnt = 0;for(int i=0;i<n;i++){if(flag[i]==0){cnt++,flag[i]=1;for(int j=i+1;j<n;j++)if(no[j].x>no[i].y&&flag[j]==0){flag[j]=1;no[i].y=no[j].y;}}}printf("%d\n",cnt*10);}return 0;
}
法二:
using namespace std;
int main()
{int s[410],n,a,b;int T;cin>>T;while(T--){memset(s,0,sizeof(s));cin>>n;for(int i=0;i<n;i++){cin>>a>>b;if(a>b) {int t=a;a = b;b = t;}if(a%2==1) a++;if(b%2==1) b++;for(int j=a;j<=b;j++)s[j]++;}sort(s,s+401);printf("%d\n",s[400]*10);}return 0;
}