Hdu 1025 - Constructing Roads In JGShining's Kingdom

用二分查找,不知为何一直RE,数组明明开的足够了。

RE代码:

#include <stdio.h>
int num[500001];
int ans[500001];
int main()
{
    int n, k=1, len, p, r, i;
    int low, mid, high;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0; i<n; i++)
        {
            scanf("%d %d",&p,&r);
            num[p] = r;
        }
        ans[len] = num[1];
        len = 1;
        for(i=1; i<=n; i++)
        {
            low = 1;
            high = len;
            while(low<=high)
            {
                mid = (low+high)/2;
                if(ans[mid] >= num[i])
                    high = mid - 1;
                else
                    low = mid + 1;
            }
            ans[low] = num[i];
            if(low>len) len++;
        }

        printf("Case %d:\n", k++);
        if(len == 1)
            printf("My king, at most 1 road can be built.\n\n");
        else
            printf("My king, at most %d road can be built.\n\n", len);
    }
    return 0;
}


 

 

你可能感兴趣的:(Hdu 1025 - Constructing Roads In JGShining's Kingdom)