Hdoj 1025 constructing roads in jgshining's Kingdom [longest incrementing subsequence]

Source: Internet
Author: User

Constructing roads in jgshining's kingdom

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 16262 accepted submission (s): 4633

Problem descriptionjgshining's kingdom consists of 2n (N is no more than 500,000) small cities which are located in two parallel lines.

Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities ). each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. you may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.

With the development of industry, poor cities wanna import resource from rich ones. the roads existed are so small that they're unable to ensure the heavy trucks, so new roads shoshould be built. the poor cities strongly BS each other, so are the rich ones. poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of Road with other rich ones. because of economics benefit, any rich city will be willing to export resource to any poor one.

Rich citis marked from 1 to n are located in line I and poor ones marked from 1 to n are located in line II.

The location of rich city 1 is on the left of all other cities, rich city 2 is on the left of all other cities excluding rich city 1, rich City 3 is on the right of rich city 1 and rich city 2 but on the left of all other cities... and so as the poor ones.

But as you know, two crossed roads may cause a lot of traffic accident so jgshining has established a law to forbid constructing crossed roads.

For example, the roads in Figure I are forbidden.



In order to build as your roads as possible, the young and handsome king of the kingdom-jgshining needs your help, please help him. ^_^
 
Inputeach test case will begin with a line containing an integer N (1 ≤ n ≤ 500,000 ). then n lines follow. each line contains two integers P and R which represents that poor city p needs to import resources from rich city R. process to the end of file.
 
Outputfor each test case, output the result in the form of sample.
You shoshould tell jgshining what's the maximal number of road (s) can be built.
 
Sample Input
21 22 131 22 33 1
 
Sample output
Case 1:My king, at most 1 road can be built.Case 2:My king, at most 2 roads can be built.HintHuge input, scanf is recommended.

Analysis: the traditional O (n2) will certainly time out, so an O (nlgn) algorithm is used. It mainly uses binary search ..

This is a good example of this algorithm: http://www.cnblogs.com/dongsheng/archive/2013/01/25/2876323.html.

Code:

#include <stdio.h>#include <string.h>#include <algorithm>#define  M 500005using namespace std;struct node{    int p, r;}s[M];int d[M];int cmp(node a, node b){    return a.p<b.p;}int bis(int s, int len){    int right = len, left = 0, mid;    while(left <= right){        mid = (left+right)>>1;        if(d[mid]>s) right = mid-1;        else if(d[mid] < s) left = mid+1;        else return mid;    }    return left;}int main(){    int n, v=1;    while(scanf("%d", &n) == 1){        memset(d, 0, sizeof(d));        int i, len;        for(i = 0; i < n; i ++)            scanf("%d%d", &s[i].p, &s[i].r);        sort(s, s+n, cmp);        len = 1;        d[len] = s[0].r;        for(i = 1; i < n; i ++){            int t = bis(s[i].r, len);            d[t] = s[i].r;            if(t > len)                len++;        }        printf("Case %d:\n", v++);        printf("My king, at most %d road", len);        if(len > 1) printf("s");        printf(" can be built.\n\n");    }    return 0;}



Hdoj 1025 constructing roads in jgshining's Kingdom [longest incrementing subsequence]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.