Humble Numbers HDU - 1058 2分dp
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的更多相关文章
- 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, ...
 - HDOJ(HDU).1058 Humble Numbers (DP)
		
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
 - hdu 1058 dp.Humble Numbers
		
Humble Numbers Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
 - HDU 1058 Humble Numbers (DP)
		
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
 - hdu 1058:Humble Numbers(动态规划 DP)
		
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
 - 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* ...
 - HDU 1058 Humble Numbers (动规+寻找丑数问题)
		
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
 - [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 ...
 - HDU 1058 Humble Number
		
Humble Number Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humbl ...
 
随机推荐
- C+++string类如何判断字符串为空
			
string类是C++STL类之一,有很丰富的接口,判断string为空是经常用到的操作. string类为空,实际也就是元素为0个. 可以按照如下方式判断: 1.string类有自己的成员函数emp ...
 - 前端开发——HTML学习笔记
			
HTML03
 - xxx did not match any file(s) known to git
			
切换分支的时候,报了标题这么个错误,error: pathspec ''xxx did not match any file(s) known to git. 看见不能切换分支,我首先 git sta ...
 - [原][粒子特效][spark]调节器modifier
			
深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html group添加modifier的方式: modifier An abstra ...
 - linux 打开一个文件现swap文件
			
转自:http://blog.csdn.net/eckelwei/article/details/17078187 有时候在用vim打开文件时提示类似以下的信息: 发现交换文件 ".expo ...
 - tensorflow学习笔记一------下载安装,配置环境(基于ubuntu16.04 pycharm)
			
一,安装TensorFlow 安装所有必备工具 sudo apt-get install python-pip python-dev python-virtualenv 安装TensorFlow su ...
 - SpringMVC获取页面表单参数的几种方式
			
以下几种方式只有在已搭好的SpringMVC环境中,才能执行成功! 首先,写一个登陆页面和一个Bean类 <%@ page language="java" co ...
 - nRF52832无法加载协议栈文件
			
使用keil向nRF52832下载程序时报错 Error:Flash Download failed-Could not load file"..\..\..\..\compoents\so ...
 - [MySQL]典型的行列转换
			
列变成行 测试数据库数据样式: 应用的sql语句: SELECT TM,NAME,SUM(GE) AS 'GE',SUM(GD) AS 'GD',SUM(CT) AS 'CT',SUM(NUM) AS ...
 - Hibernate多对多单向关联和双向关联 --Hibernate框架
			
Hibernate关联关系中相对比较特殊的就是多对多关联,多对多关联与一对一关联和一对多关联不同,多对多关联需要另外一张映射表用于保存多对多映射信息.本例介绍多对多单向关联和双向关联.单向关联 :指具 ...