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. ^_^

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

Hint

Huge input, scanf is recommended.

二分加dp
dp[i]表示第i个上升子序列的最小值;
num[i]
dp[1]
dp[2]
dp[3]
dp[4]
然后去找到num[i]
在的位子用2分
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int num[];
int n;
int dp[];
struct node
{
int a,b;
}lu[];
int main()
{
int k=;
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
{
scanf("%d%d",&lu[i].a,&lu[i].b);
num[lu[i].a]=lu[i].b;
}
memset(dp,,sizeof dp);
int res=;
for(int i=;i<=n;i++)
{
if(num[i]>dp[res])
{
dp[++res]=num[i];
}
else
{
int l=,r=res;
while(res>=l)
{
int mid=(r+l)/;
if(num[i]>dp[mid]&&num[i]<=dp[mid+])
{
dp[mid+]=num[i];
break;
}
else if(num[i]<=dp[mid])
{
r=mid-;
}
else
l=mid+; }
}
}
if(res==)
printf("Case %d:\nMy king, at most %d road can be built.\n",++k,res);
else
printf("Case %d:\nMy king, at most %d roads can be built.\n",++k,res);
printf("\n"); }
return ;
}

Humble Numbers HDU - 1058 2分dp的更多相关文章

  1. Humble Numbers HDU - 1058

    A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...

  2. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  3. hdu 1058 dp.Humble Numbers

    Humble Numbers Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  4. HDU 1058 Humble Numbers (DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. hdu 1058:Humble Numbers(动态规划 DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. HDU 1058 Humble Numbers【DP】

    题意:给出丑数的定义,只含有2,3,5,7这四个素数因子的数称为素数.求第n个丑数. 可以先观察几个丑数得出规律 1:dp[1] 2:min(1*2,1*3,1*5,1*7) 3:min(2*2,1* ...

  7. HDU 1058 Humble Numbers (动规+寻找丑数问题)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  8. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  9. HDU 1058 Humble Number

    Humble Number Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humbl ...

随机推荐

  1. 【译】第4节---简单的Code First示例

    原文地址:http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx 假设我们要为XYZ学校创建一 ...

  2. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  3. jquery 获取元素(父节点,子节点,兄弟节点),元素筛选

    一, js 获取元素(父节点,子节点,兄弟节点)   var test = document.getElementById("test"); var parent = test.p ...

  4. [转]Eclipse下开发Struts奇怪异常:org.apache.struts.taglib.bean.CookieTei

    今天早上开始在Eclipse下学习struts,于是按照李兴华老师的<struts入门视频教程>一步一步地充满快乐的学习,等把登陆程序写完,打开浏览器准备运行的时候,奇怪的异常产生了,异常 ...

  5. 由设置body线性背景色引发的问题-----当声明文档类型时,对body设置线性背景色,页面背景色无法整体线性过渡

    问题:当声明文档类型时,对body设置线性背景色,页面背景色无法整体线性过渡 不声明文档类型时,对body设置线性背景色 <HTML> <head> <meta char ...

  6. IO模型——IO多路复用机制

    (1)I/O多路复用技术通过把多个I/O的阻塞复用到同一个select.poll或epoll的阻塞上,从而使得系统在单线程的情况下可以同时处理多个客户端请求.与传统的多线程/多进程模型比,I/O多路复 ...

  7. Git 基础 - 打标签

    列出现有标签(或者使用git tag -l) $ git tag v0. v1. 如果只对 1.4.2 系列的版本感兴趣 $ git tag -l 'v1.4.2.*' v1. v1. v1. v1. ...

  8. flutter安装与配置 v1.2.1版本

    1---- 上面是下载地址https://flutter.dev/docs/development/tools/sdk/archive#windows 2---- 下载后,解压安装到C盘 3--- 测 ...

  9. EditText取消焦点

    EditText取消焦点: 在父容器添加: android:focusable="true" android:focusableInTouchMode="true&quo ...

  10. 使用Qpaint在图片上写文字

    开发过程中需要实现在图片上叠加文字,可以采用Qpaint在图片上写文字,然后将图片显示在上面.再将Qlabel加到Qwidget中.效果如下 //创建对象,加载图片 QPixmap pix; pix. ...