hdu1025 最长不下降子序列nlogn算法
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
Hint
Description
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. ^_^
Input
Output
You should tell JGShining what's the maximal number of road(s) can be built.
Sample Input
1 2
2 1
3
1 2
2 3
3 1
Sample Output
My king, at most 1 road can be built.
Case 2:
My king, at most 2 roads can be built.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=+;
int dp[maxn];
int road[maxn];
int binary(int n,int m)
{
int l=,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(m>=dp[mid]) l=mid+;
else r=mid-;
}
return l;
}
int kase=;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a,b;
memset(road,,sizeof(road));
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
road[a]=b;
}
dp[]=road[];
int len=;
for(int i=;i<=n;i++)
{
int t=binary(len,road[i]);
//cout<<t<<endl;
dp[t]=road[i];
if(t>len) len++;
}
printf("Case %d:\n",++kase);
if(len==) printf("My king, at most 1 road can be built.\n\n");
else printf("My king, at most %d roads can be built.\n\n",len);
}
}
hdu1025 最长不下降子序列nlogn算法的更多相关文章
- 最长不下降子序列nlogn算法详解
今天花了很长时间终于弄懂了这个算法……毕竟找一个好的讲解真的太难了,所以励志我要自己写一个好的讲解QAQ 这篇文章是在懂了这个问题n^2解决方案的基础上学习. 解决的问题:给定一个序列,求最长不下降子 ...
- 最长不下降子序列 nlogn && 输出序列
最长不下降子序列实现: 利用序列的单调性. 对于任意一个单调序列,如 1 2 3 4 5(是单增的),若这时向序列尾部增添一个数 x,我们只会在意 x 和 5 的大小,若 x>5,增添成功,反之 ...
- 最长不下降子序列nlogn
b[i]表示长度为i的最长不下降子序列的最小末尾元素的值显然它是单调递增的,满足二分性质,然后就可以愉快地二分啦. #include<iostream> #include<cstdi ...
- 最长不下降序列nlogn算法
显然n方算法在比赛中是没有什么用的(不会这么容易就过的),所以nlogn的算法尤为重要. 分析: 开2个数组,一个a记原数,f[k]表示长度为f的不下降子序列末尾元素的最小值,tot表示当前已知的最长 ...
- 最长不下降子序列的O(n^2)算法和O(nlogn)算法
一.简单的O(n^2)的算法 很容易想到用动态规划做.设lis[]用于保存第1~i元素元素中最长不下降序列的长度,则lis[i]=max(lis[j])+1,且num[i]>num[j],i&g ...
- 最长不下降子序列 (O(nlogn)算法)
分析: 定义状态dp[i]表示长度为i的最长不下降子序列最大的那个数. 每次进来一个数直接找到dp数组第一个大于于它的数dp[x],并把dp[x - 1]修改成 那个数.就可以了 AC代码: # in ...
- 算法进阶 (LIS变形) 固定长度截取求最长不下降子序列【动态规划】【树状数组】
先学习下LIS最长上升子序列 看了大佬的文章OTZ:最长上升子序列 (LIS) 详解+例题模板 (全),其中包含普通O(n)算法*和以LIS长度及末尾元素成立数组的普通O(nlogn)算法,当然还 ...
- tyvj 1049 最长不下降子序列 n^2/nlogn
P1049 最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 ...
- 最长不下降子序列 O(nlogn) || 记忆化搜索
#include<stdio.h> ] , temp[] ; int n , top ; int binary_search (int x) { ; int last = top ; in ...
随机推荐
- get set
关于C# get set的文章很多,但是笔者的这篇文章有它的特别之处,笔者用简单的语言把c# get set讲述的十分明了. C# get set释一:属性的访问器包含与获取(读取或计算)或设置(写) ...
- poj 2195Going Home
http://poj.org/problem?id=2195 #include<cstdio> #include<cstring> #include<cmath> ...
- 基于MINA框架快速开发网络应用程序
1.MINA框架简介 MINA(Multipurpose Infrastructure for Network Applications)是用于开发高性能和高可用性的网络应用程序的基础框架.通过使用M ...
- Wormholes 最短路判断有无负权值
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- 【转】android 4.3 BLE onCharacteristicWrite没有回调
原文网址:http://bbs.csdn.net/topics/390882717?page=1 问题1.我在自己程序有开一个Timer定时去readCharacteristic, 每次read可以成 ...
- (转载)php的类中可以不定义成员变量,直接在构造方法中使用并赋值吗?
(转载)http://s.yanghao.org/program/viewdetail.php?i=184313 php的类中可以不定义成员变量,直接在构造方法中使用并赋值吗? class block ...
- cf701C They Are Everywhere
Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in ...
- Test execution order
刚开始的时候,JUnit并没有规定测试方法的调用执行顺序.方法通过映射的API返回的顺序进行调用.然 而,使用JVM顺序是不明智的,因为Java平台没有规定任何特定的顺序,事实上JDK7或多或少的返回 ...
- Java程序打包
包名为 com.longneo.data,主程序入口为:com.longneo.data.AppMain 1.到工程目录下 /bin 执行 jar -cvf Demo.jar com 执行完之后会生成 ...
- XCode7中不能使用http的临时配置解决办法
先看看iOS9新特性中关于ATS的官方文档: App Transport Security App Transport Security (ATS) enforces best practices i ...