1001 Who is the best?
#include<cstdio>
#include<cstring>
#define MAX 1000
int a[MAX],b[MAX];
int main(){
int T,n;
scanf("%d",&T);
while (T--){
memset(b,0,sizeof(b));
scanf("%d",&n);
for (int i = 0; i<n; i++){
scanf("%d",&a[i]);
b[a[i]]++;
}
int maxn = 0,ans;
for (int i = 1; i<=n; i++) if (b[i] > maxn){
maxn = b[i];
ans = i;
}
printf("%d\n",ans);
}
return 0;
}
1002 lines
题目比较简单,只要所有的点按从小到大的顺序排一遍,并记录号节点是区间的起始节点还是终止节点,在排序的过程中注意如果两个端点的坐标大小相同时,应按照起点优先的原则。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 500000
using namespace std;
struct Node{
int x;
bool sign;
bool operator < (const Node& rhs) const{
if (x != rhs.x) return rhs.x > x;
return rhs.sign < sign;
}
}p[MAX];
int main(){
int T,n;
scanf("%d",&T);
while (T--){
scanf("%d",&n);
int u,v,cnt = 0;
for (int i = 0; i<n; i++){
scanf("%d%d",&u,&v);
p[cnt++] = (Node){u,true};
p[cnt++] = (Node){v,false};
}
sort(p,p+cnt);
//for (int i = 0; i<cnt; i++) printf("%d %d\n",p[i].x,p[i].sign);
int ans = 0, res = 0;
for (int i = 0; i<cnt; i++){
if (p[i].sign){
res++;
ans = max(ans,res);
}
else{
res--;
}
}
printf("%d\n",ans);
}
return 0;
}