Hdu 1025(LIS)
Constructing Roads In JGShining's Kingdom
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15126 Accepted Submission(s): 4300Problem 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 should 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 economic 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 many 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 should tell JGShining what's the maximal number of road(s) can be built.Sample Input2
1 2
2 1
3
1 2
2 3
3 1Sample OutputCase 1:
My king, at most 1 road can be built.Case 2:
My king, at most 2 roads can be built.
/*************************************************************************
> File Name: 1025.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年07月16日 星期三 23时41分28秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAX_N = +;
int n;
int dp[MAX_N], A[MAX_N]; int
main(void) {
int c = ;
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++) {
int p, r;
scanf("%d %d", &p, &r);
A[p] = r;
}
int len = ;
dp[] = A[];
for (int i = ; i <= n; i++) {
int low = , high = len;
while (low <= high) {
int mid = (low + high) / ;
if (dp[mid] > A[i]) high = mid - ;
else if (dp[mid] < A[i]) low = mid + ;
}
dp[low] = A[i];
if (low > len) len++;
}
printf("Case %d:\n", c++);
if ( == len) puts("My king, at most 1 road can be built.");
else printf("My king, at most %d roads can be built.\n", len);
puts("");
} return ;
}
Hdu 1025(LIS)的更多相关文章
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...
- hdu 1025 lis 注意细节!!!【dp】
感觉这道题浪费了我半个小时的生命......哇靠!原来输出里面当len=1时是road否则是roads!!! 其实做过hdu 1950就会发现这俩其实一样,就是求最长上升子序列.我用结构体记录要连线的 ...
- HDU 1025 (LIS+二分) Constructing Roads In JGShining's Kingdom
这是最大上升子序列的变形,可并没有LIS那么简单. 需要用到二分查找来优化. 看了别人的代码,给人一种虽不明但觉厉的赶脚 直接复制粘贴了,嘿嘿 原文链接: http://blog.csdn.net/i ...
- Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- HDU 1025:Constructing Roads In JGShining's Kingdom(LIS+二分优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Problem Des ...
- hdu 1025 上面n个点与下面n个点对应连线 求最多能连有多少条不相交的线 (LIS)
题目大意有2n个城市,其中有n个富有的城市,n个贫穷的城市,其中富有的城市只在一种资源富有,且富有的城市之间富有的资源都不相同,贫穷的城市只有一种资源贫穷,且各不相同,现在给出一部分贫穷城市的需求,每 ...
随机推荐
- 基础类型转化成String 转
基础类型转化成String 在程序中你可能时常会需要将别的类型转化成String,有时候可能是一些基础类型的值.在拼接字符串的时候,如果你有两个或者多个基础类型的值需要放到前面,你需要显式的将第一个值 ...
- 设置苹果手机input按钮和button按钮颜色显示问题
网页上,尽管设置了input按钮和button的背景颜色,但在苹果手机上测试时仍会发现背景颜色为透明渐变色,不起作用,怎么解决呢? 在css公共样式中加上下面代码就可以解决,去除button和inpu ...
- 对话框处理Enter,Esc键相应问题
在类视图里面选择你要实现的类,右键属性,在属性里面找到函数PreTranslateMessage,然后添加PreranslateMessage的消息函数,在PreTranslateMessage的消息 ...
- Spark运行架构详解
原文引自:http://www.cnblogs.com/shishanyuan/p/4721326.html 1. Spark运行架构 1.1 术语定义 lApplication:Spark Appl ...
- Windows API 第二篇 SHGetSpecialFolderPath
BOOL SHGetSpecialFolderPath( HWND hwndOwner, LPTSTR lpszPath, ...
- 学习Python:StringIO与cStringIO
StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO.一个简单的例子,让你对StringIO有一个感性的认识: f ...
- HZOI20190902模拟35题解
题面: A:公园 DAG上想拓扑dp 然而博主记忆化搜索了一下 设f[i][j]表示从i节点走j个点出公园所用的最小时间 则$f[u][i]=min(f[v][j-1]+dis_{u,v})$; 然后 ...
- html css javascript mysql php学习总结
一. html:超文本标记语言,运行在浏览器上,由浏览器解析 1.格式 <!doctype html> 声明文档类型,说明html版本号 <html> 说明代码格式 <h ...
- PAT甲级——A1020 Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 给NetBeans配置javafx环境
JavaFX开发环境安装配置,这里给大家介绍一个非常有用的步骤 从Java8开始,JDK(Java开发工具包)包括了JavaFX库. 因此,要运行JavaFX应用程序,您只需要在系统中安装Java8或 ...
