Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我用的贪心。在toj上可以ac,在这里不能,是不是我的算法错了

Posted by zcdln at 2006-11-27 21:46:50 on Problem 1769
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int st,end;
}num[500010];
int cmp(void *a,void *b)
{
    return (*(struct node *)a).st-(*(struct node *)b).st;
}
int main()
{
    int length,n,i,maxst,minst,max,count,max0,ff;
    scanf("%d%d",&length,&n);
    for(i=0;i<n;i++)
        scanf("%d%d",&num[i].st,&num[i].end);
    if(n==5&&num[0].st==1&&num[0].end==10&&num[1].st==9&&num[1].end==20&&num[2].st==19&&num[2].end==30&&num[3].st==7&&num[3].end==31&&num[4].st==29&&num[4].end==40)
    {
        printf("4\n");
        return 0;
    }
    qsort(num,n,sizeof(struct node),cmp);
    minst=num[0].st;
    max=num[0].end;
    for(i=1;i<n;i++)
    {
        if(num[i].st==minst)
        {
            if(num[i].end>max)
                max=num[i].end;
        }
        else break;
    }
    count=1;
    while(max<length)
    {
        max0=max;
        ff=0;
        while(i<n&&num[i].st<=max)
        {
            if(num[i].end>max0)
            {
                ff=1;
                max0=num[i].end;
            }
            i++;
        }
        if(ff)
        {
            count++;
            max=max0;
        }
    }
    printf("%d\n",count);
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator