HDU 1025 Constructing Roads In JGShining's Kingdom (DP)
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. ^_^
You should tell JGShining what's the maximal number of road(s) can be built.
2
1 2
2 1
3
1 2
2 3
3 1
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.假设你想是二分匹配就错了,事实上是个LIS。。。题意:穷国连接富国,不能交叉,问最大的对数注意输出的road(len=1)和roads(len>1)。#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int maxn=500000+50;
int a[maxn],num[maxn];
int main()
{
int n,x,y;
int cas=0;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
a[x]=y;//穷国x的值为y,求最大LIS(最大上升子列)
}
num[1]=a[1];
int len=1;
int l,r,mid,ans;
for(int i=2;i<=n;i++)//LIS的优化写法
{
l=1;
r=len;
while(l<=r)
{
mid=(l+r)>>1;
if(num[mid]>=a[i])
r=mid-1;
else
l=mid+1;
}
num[l]=a[i];
if(l>len)
len++;
}
printf("Case %d:\n",++cas);
if(len==1) printf("My king, at most %d road can be built.\n\n",len);
else printf("My king, at most %d roads can be built.\n\n",len);
}
return 0;
}
HDU 1025 Constructing Roads In JGShining's Kingdom (DP)的更多相关文章
- hdu1025 Constructing Roads In JGShining's Kingdom(二分+dp)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...
- 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(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom[动态规划/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(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
随机推荐
- GoogleCode新手教程
GoogleCode页面介绍 Project Home 首先显示的是project home,页面左边的是这个项目的介绍,右边的License是说明使用的是什么开源协议,Labels是标签的意思,就是 ...
- jquery获取css color 值返回RGB
css代码如下: a, a:link, a:visited { color:#4188FB; } a:active, a:focus, a:hover { color:#FFCC00; } js代码如 ...
- 安装python-MySQLdb 出现error: command 'gcc' failed with exit status 1的解决方法
>>> yum install MySQL-p* >>>yum install python-devel >>>cd MySQL-python-1 ...
- Successfully installed matplotlib
Installing /usr/local/lib/python2.7/dist-packages/matplotlib-1.4.0-py2.7-nspkg.pthSuccessfully insta ...
- 通过javascript实现1~100内能同时被2和3整除的数并生成如下表格
请通过javascript实现1~100内能同时被2和3整除的数并生成如下表格: <!DOCTYPE html><html lang="en"><he ...
- 非常好用的正则表达式"\\s+" - 匹配任意空白字符
说起来,博主使用过的正则场景虽然不多,但是就是在这当中,我发现"\\s+"真好用! 详解 "\\s+" 正则表达式中\s匹配任何空白字符,包括空格.制表符.换页 ...
- HTML+JS版本的俄罗斯方块
<!doctype html><html><head></head><body> <div id="box" st ...
- matlab常用小函数(二)
numel 元素个数 assert 表达式为假时输出某个字符串 int2str 整形转化为字符串型 numel(A) 返回A中的元素个数,A可以是任何的数据结构,如向量.矩阵.元胞.结构体等 asse ...
- jquery判断邮箱格式问题
方法一: var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/; var email_val = $("#Email").val(); ...
- bzoj 3143: [Hnoi2013]游走 高斯消元
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1026 Solved: 448[Submit][Status] ...