HDUOJ lines

来源:互联网 发布:通达oa迁移linux 编辑:程序博客网 时间:2024/06/11 20:56
/*linesTime Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 590    Accepted Submission(s): 277Problem DescriptionJohn has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. InputThe first line contains a single integer T(1≤T≤100)(the data for N>100 less than 11 cases),indicating the number of test cases.Each test case begins with an integer N(1≤N≤105),indicating the number of lines.Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109),describing a line.  OutputFor each case, output an integer means how many lines cover A. Sample Input251 2 2 22 43 45 100051 12 23 34 45 5 Sample Output31 Source BestCoder Round #20  */#include<stdio.h>#include<string.h>#include<stdlib.h>int comp(const void *a,const void *b){return *(int *)a-*(int *)b;}int a[100005],b[100005];int main(){int cases,j,i,temp_j,temp_i,p,cnt,max,n;scanf("%d",&cases);while(cases--){memset(a,-1,sizeof(a));memset(b,-1,sizeof(b));max=-1,cnt=0;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d%d",&a[i],&b[i]);}qsort(a,100005,sizeof(int),comp);qsort(b,100005,sizeof(int),comp);for(j=0;;j++){if(b[j]!=-1) break;}temp_j=j;for(i=0;i<100005;i++){if(a[i]==-1) continue;if(a[i]>b[j])            {                cnt--;                j++;                temp_j=j;                break;            }else{    p=1;cnt++;temp_i=i;if(cnt>max) max=cnt;while(p){if(a[++temp_i]==a[i]){break;}while(1){if(b[temp_j++]==a[i]) {cnt--;j++;temp_j=j;}else {p=0;temp_j=j;break;}}}}}printf("%d\n",max);}return 0;}//这题是一次bc的b题,下面是大仙的代码,和我的想法一样, 不过他用结构体标记左右端点,再混合排序,而我对结构体不熟悉//没想到用这个所以就用a,b数组讨论成数组的形式,思维情况比较复杂,wa了5,6次。最后还是ac了 ,时间和大仙的那个差不多,差了100ms左右。 #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct line{    int x;    int flag;}L[200010];bool cmp(line a,line b){    if(a.x<b.x||(a.x==b.x&&a.flag<b.flag)) return 1;    return 0;}int main(){    int _,n,all,a,b;    scanf("%d",&_);              //用_作为循环变量,值得学习。     while(_--){        scanf("%d",&n);        all=0;        for(int i=1;i<=n;i++){            scanf("%d%d",&a,&b);            L[all].x=a;            L[all++].flag=0;            L[all].x=b;            L[all++].flag=1;        }        sort(L,L+all,cmp);        int total=0;        int ma=0;        for(int i=0;i<all;i++){            if(L[i].flag==0)                total++;            else total--;            if(total>ma)                ma=total;        }        printf("%d\n",ma);    }    return 0;}//大仙的ac的代码。 

0 0