C - DP

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 1025 uDebug

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

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 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

Each 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.
 

Output

For 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 Input

2
1 2
2 1
3
1 2
2 3
3 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.

思路分析:稍加分析就知道是最长不下降子序列
但是以前都是n2的复杂度,这样写势必药丸,百度了一下才知道有nlogn的写法dp+二分
dp数组存的是当前最长的最长不下降子序列,数组内保证递增,因此可以二分找下一个数字
需要插入的位置
tips:注意输出格式,road和roads区别,还有要输出两个换行
代码:
#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算法的更多相关文章

  1. 最长不下降子序列nlogn算法详解

    今天花了很长时间终于弄懂了这个算法……毕竟找一个好的讲解真的太难了,所以励志我要自己写一个好的讲解QAQ 这篇文章是在懂了这个问题n^2解决方案的基础上学习. 解决的问题:给定一个序列,求最长不下降子 ...

  2. 最长不下降子序列 nlogn && 输出序列

    最长不下降子序列实现: 利用序列的单调性. 对于任意一个单调序列,如 1 2 3 4 5(是单增的),若这时向序列尾部增添一个数 x,我们只会在意 x 和 5 的大小,若 x>5,增添成功,反之 ...

  3. 最长不下降子序列nlogn

    b[i]表示长度为i的最长不下降子序列的最小末尾元素的值显然它是单调递增的,满足二分性质,然后就可以愉快地二分啦. #include<iostream> #include<cstdi ...

  4. 最长不下降序列nlogn算法

    显然n方算法在比赛中是没有什么用的(不会这么容易就过的),所以nlogn的算法尤为重要. 分析: 开2个数组,一个a记原数,f[k]表示长度为f的不下降子序列末尾元素的最小值,tot表示当前已知的最长 ...

  5. 最长不下降子序列的O(n^2)算法和O(nlogn)算法

    一.简单的O(n^2)的算法 很容易想到用动态规划做.设lis[]用于保存第1~i元素元素中最长不下降序列的长度,则lis[i]=max(lis[j])+1,且num[i]>num[j],i&g ...

  6. 最长不下降子序列 (O(nlogn)算法)

    分析: 定义状态dp[i]表示长度为i的最长不下降子序列最大的那个数. 每次进来一个数直接找到dp数组第一个大于于它的数dp[x],并把dp[x - 1]修改成 那个数.就可以了 AC代码: # in ...

  7. 算法进阶 (LIS变形) 固定长度截取求最长不下降子序列【动态规划】【树状数组】

    先学习下LIS最长上升子序列 ​ 看了大佬的文章OTZ:最长上升子序列 (LIS) 详解+例题模板 (全),其中包含普通O(n)算法*和以LIS长度及末尾元素成立数组的普通O(nlogn)算法,当然还 ...

  8. tyvj 1049 最长不下降子序列 n^2/nlogn

    P1049 最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 ...

  9. 最长不下降子序列 O(nlogn) || 记忆化搜索

    #include<stdio.h> ] , temp[] ; int n , top ; int binary_search (int x) { ; int last = top ; in ...

随机推荐

  1. iOS证书快要过期怎么办?

    说法一: 1.先revoke你的Certificate,重新生成一个新的. 2.Edit一下你的证书,选择新的Certificate. 3.下载覆盖之前的证书,就可以了. 这个帐号发布的产品不会受到影 ...

  2. Altera quartus II遇到的问题

    编译时提示: Warning (13024): Output pins are stuck at VCC or GND Warning (13410): Pin "SCLK" is ...

  3. Multiplication Table

    CF#256D:http://codeforces.com/contest/448/problem/D 题意:给你一个n*m的表格,第i行第j列的数据是i*j,然后询问第k小的数,这里的排序是不去重的 ...

  4. cf C. Hamburgers

    http://codeforces.com/contest/371/problem/C 二分枚举最大汉堡包数量就可以. #include <cstdio> #include <cst ...

  5. Android自定义ActionBar

    Android 3.0及以上已经有了ActionBar的API,可以通过引入support package在3.0以下的平台引用这些API,但这儿呢,完全自定义一个ActionBar,不用引入额外ja ...

  6. @action 注解

    ================================================= 下载 注解配置 private String fileName; private String co ...

  7. javascript 四舍五入

    原生 javascript 中四舍五入的函数 toFixed(n) , n为要保留的小数位数. (0<= n <=20) var num=1.0999; console.log(num.t ...

  8. Hive从概念到安装使用总结

    一.Hive的基本概念 1.1 hive是什么? (1)Hive是建立在hadoop数据仓库基础之上的一个基础架构: (2)相当于hadoop之上的一个客户端,可以用来存储.查询和分析存储在hadoo ...

  9. java入门之异常处理小结

    (1)异常和错误 异常(Exception):不正常的事件,会中断程序,在运行中发生的意外,程序本身可以处理,防止程序中断:程序中会捕获的异常信息,并告诫用程序员,不要求程序必须对它做处理.catch ...

  10. Python学习笔记2-flask-sqlalchemy 简单笔记

    flask-sqlalchemy 简单笔记 字数 阅读 评论 喜欢 flask-sqlalchemy SQLAlchemy已经成为了python世界里面orm的标准,flask是一个轻巧的web框架, ...