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. python学习 day09打卡 初识函数

    本节内容: 1.什么是函数 2.函数定义,函数名,函数体及函数的调用 3.函数的返回值 4.函数的参数 一.什么是函数 函数:对代码块和功能的封装和定义 定义一个事情或者功能.等到需要的时候直接去用, ...

  2. 【Java】【图形】

    /* 栗子 了解swing */import javax.swing.*;public class test_swing extends JFrame { //继承JFrame顶层容器类(可以添加其他 ...

  3. 转一篇 ShaderVariantCollection介绍的比较详细的文章 感谢作者

    http://www.seven-fire.cn/archives/174 Unity3D Shader加载时机和预编译 焱燚(七火)  |    2016年7月6日  |   UnityShader ...

  4. hdu 3094 A tree game 树上sg

    A tree game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  5. 启动tomcat 报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

    [root@localhost META-INF]# systemctl start tomcat Job for tomcat.service failed because the control ...

  6. centos7 下载安装tomcat9

    需要Java环境 https://www.cnblogs.com/sea-stream/p/10404360.html 官网下载安装包 wget http://archive.apache.org/d ...

  7. mysql5.7.23手动配置安装windows版

    1.mysql下载地址 官网:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 官网我下载的是: 百度网盘:链接: https://pa ...

  8. 设计模式(二)策略模式(Strategy)

    1.分析项目中变化部分与不变部分 2.多用组合,少用继承:用行为类组合,而不是行为的继承 案例: 第一步,把行为抽象为接口 package top.littlepage.designPattern.S ...

  9. 从DFS到记忆化DFS到动态规划

    什么是动态规划? 动态规划(Dynamic Programming)是通过组合子问题的解来解决问题的.动态规划是用于求解包含重叠子问题的最优化问题的方法.其基本思想是,将原问题分解为相似的子问题.在求 ...

  10. OpenModelica中simulate的用法

    先把官网上的说明文档放上来: simulate simulates a modelica model by generating c code, build it and run the simula ...