ACM.hdu1025

to get the ans of how many roads at most that can be built between two line without intersection of roads,we need sort the input sequence at ont edge and then deal with anther line with LIS

way,meanwhile, not forget using the nlogn way.

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 const int N=500009;
 5 struct LINE
 6 {
 7     int from,to;
 8     friend bool operator<(LINE A,LINE B)
 9     {
10         return A.from<B.from;
11     }
12 };
13 LINE node[N];
14 int y[N];
15 int c[N];
16 int MID(int a[],int value,int size)
17 {
18     int l=1,r=size;
19     int mid=(l+r)/2;
20     while(l<r)//1 3 5 7 9 3
21     {
22         if(a[mid]<value&&value<=a[mid+1])return mid+1;
23         else if(value<=a[mid])r=mid;
24         else l=mid+1;
25         mid=(l+r)/2;
26     }
27 }
28 int LIS(int len)
29 {
30     int size=1;
31     int zhong=0;
32     c[1]=y[1];
33     for(int i=2;i<=len;i++)//7 6 4 9 11 14 17 3 15 20 4 5 6 7 8
34     {//3 9 11 15 17 20
35         if(y[i]<=c[1])c[1]=y[i];
36         else if(y[i]>c[size])
37         c[++size]=y[i];
38         else
39         {
40         zhong=MID(c,y[i],size);
41         c[zhong]=y[i];
42         }
43     }
44     return size;
45 }
46 int main()
47 {
48     int n;
49     int cnt=1;
50     while(cin>>n)
51     {
52         for(int i=0;i<n;i++)
53         scanf("%d %d",&node[i].from,&node[i].to);
54         sort(node,node+n);//sort one side by ascending direction
55         for(int i=0;i<n;i++)
56             y[i+1]=node[i].to;
57         int ans=LIS(n);
58         cout<<"Case "<<cnt<<‘:‘<<endl;
59         cout<<"My king, at most "<<ans;
60         if(ans>1)cout<<" roads can be built."<<endl;
61         else cout<<" road can be built."<<endl;
62         cnt++;
63         cout<<endl;
64     }
65     return 0;
66 }

It has used the longest increasing subsequence algorithm way,and to get it solved after you have learned it.

input : with a n,represent n pairs of integers followed.And n pairs of intergers followed

output:

Cast i:

My king, at most n road(s) can be built

A sample below:

11
2 5
3 6
4 2
5 1
6 9
7 11
8 7
9 12
10 8
11 4
12 10
Case 1:
My king, at most 6 road can be built.

ACM.hdu1025

时间: 2024-10-11 16:06:26

ACM.hdu1025的相关文章

hdu1025 Constructing Roads In JGShining&#39;s Kingdom

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem Description JGShining'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 a

hdu1025 Constructing Roads In JGShining&amp;#39;s Kingdom(二分+dp)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem Description JGShining'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 a

《ACM/ICPC 算法训练教程》读书笔记一之数据结构(堆)

书籍简评:<ACM/ICPC 算法训练教程>这本书是余立功主编的,代码来自南京理工大学ACM集训队代码库,所以小编看过之后发现确实很实用,适合集训的时候刷题啊~~,当时是听了集训队final的意见买的,感觉还是不错滴. 相对于其他ACM书籍来说,当然如书名所言,这是一本算法训练书,有着大量的算法实战题目和代码,尽管小编还是发现了些许错误= =,有部分注释的语序习惯也有点不太合我的胃口.实战题目较多是比较水的题,但也正因此才能帮助不少新手入门,个人认为还是一本不错的算法书,当然自学还是需要下不少

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

推荐acm题目

杭电  http://acm.hdu.edu.cn/onlineuser.php. 浙大  http://acm.zju.edu.cn/onlinejudge/submit.do?problemId=1http://poj.org/status惟一的阿福 password:14420121 263273 14420121 学校原来网站      http://10.1.5.253:8080/acmhome/welcome.do?method=index

ACM比赛经验

ACM比赛经验: 推荐此篇文章打印,与模板放在一起. 1. 比赛中评测会有些慢,偶尔还会碰到隔10分钟以上才返回结果的情况,这段时间不能等结果,必须开工其他题,如果WA,两道题同时做.交完每道题都要先打印. 2. 比赛时发的饭不是让你当时就吃的,那是给你赛后吃的.基本上比赛中前几名的队都没人吃,除非领先很多. 3. 很多选手,尤其是第一次参加比赛的,到一个新环境,全当旅游了,参观的参观,找同学的找同学,玩玩乐乐就把正事抛到脑后了,结果比赛自然没什么好成绩,这样的例子太多了.所以到参赛地后要时刻不

HDU 3296 &amp; POJ 3138 Acm Team Section(数学)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=3296 POJ:  http://poj.org/problem?id=3138 Acm Team Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 159    Accepted Submission(s): 47

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ