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. Json序列化提示缺少编译器要求的成员“ystem.Runtime.CompilerServices.ExtensionAttribute..ctor”

    //缺少编译器要求的成员“ystem.Runtime.CompilerServices.ExtensionAttribute..ctor” namespace System.Runtime.Compi ...

  2. 用Github做一个静态网页(GithubPages)

    一.新建一个仓库(new). 二.命名Repository name为:(名字).github.io(一定要有.github.io). 三.勾选Initialize this repository w ...

  3. SpringBoot整合+logback日志配置

    本次演示的代码结构如下,基于maven,整合SpringBoot.Spring.Mybaits的SSM框架.同时测试logback日志框架的使用及配置. 1.创建maven工程,修改pom.xml文件 ...

  4. js 动画效果实现

    1. 实现方式 - 应用场景 自己写 - 简单的.不用 jq 的项目 jq - 普通动画 成熟插件 - 复杂动画 2. 相关文章 JavaScript基于时间的动画算法 九种原生js动画效果 Twee ...

  5. AD原理图统一命名

    1 Tools->Annotate Schematics 调出统一命名窗口 2 勾选要统一命名的原理图 3 Update Changes List 4 Accept Changes(creat ...

  6. Creazy Ideas 智能汽车和智能交通

    1.现在无人驾驶靠的是摄像头监测障碍物,计算可行路线(这个结论是看到相关视频中百度无人驾驶测试中大大的摄像头推测的,非确切信息).而非像人一样对客观事物的认知学习得到障碍物的信息,如果能将AI融入进去 ...

  7. 零点红旗echarts

    当月销量 当月同比 当月市场份额 区域占比 累计销量 累计同比 累计市场份额 区域占比 lists = { district:[ {name: '东北', value: [20100,'+13%',' ...

  8. Unity --- MeshRenderer之网格合并

    创建如图所示的对象结构,parent为空对象,然后将下面的代码挂载到parent对象上运行即可. using UnityEngine; using System.Collections; public ...

  9. lua --- unpack

    unpack 返回数组中的所有元素,包括 nil,注意是数组,对于 k-v 是不返回的!!! do , ,o = } print(unpack(tab)) --默认从索引 1 开始 )) --从索引 ...

  10. lua中的闭包概念的学习笔记

    1.闭包的由来: 个人理解,lua中之所以出现闭包的概念,完全是因为lua中允许函数的嵌套定义,并且在内嵌函数中使用了外包函数中定义的局部变量,例如c.c#就不允许函数的嵌套定义(但是允许函数的嵌套调 ...