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 ...
随机推荐
- JaveWeb 公司项目(6)----- 通过ToolTip给控件添加动态注释
现在公司的项目进展到了视屏这一块,关于海康网页端的构建我会另外写一篇博客来详细讲解,这一篇的博文主要讲的是我刚刚遇到的一个小问题 连接上了视屏之后,将控制按钮换成图标,方位按钮比较好理解,调焦调距的按 ...
- JDK10 新特性
关于至此,我从大一下学习,以及大二上的巩固,这应该是SE部分的最后一章节内容,介绍一下jdk10的新特性 jdk在更新10之后,出现很多新特性,根据我所观看的视频,主要提及以下几点新特性 1.新增va ...
- 一篇很好的java异常框架讲解
https://www.cnblogs.com/itcui/p/6400499.html 其实原作者是csdn的一名博主,实在受不了csdn的广告,所以就摘录这篇
- MVC后台获取数据和插入数据的三种方式【二】
MVC模式下,从前端获取数据返回后台,总共有三种形式.下面的代码示例将演示如何将数据返回到后端. 一.首先我们看看表单代码,注意input标签中name的值. <html> <hea ...
- js,java时间处理
1.JS获取时间格式为“yyyy-MM-dd HH:mm:ss”的字符串 function getTimeStr(){ var myDate = new Date(); var year = myDa ...
- openvswitch vlan下的虚拟机与物理机通信
1,安装openvswitch ,图形界面显示等用到的安装包. yum install libvirt openvswitch python-virtinst xauth tigervnc -y 2, ...
- CentOS/redhat使用光盘镜像源
1,首先进行光盘的挂载,注意光盘挂载时不会自动建立目录的, 所以需要自己建立目录. mkdir /mnt/cdrom mount /dev/cdrom /mnt/cdrom #de ...
- 算法笔记--字典树(trie 树)&& ac自动机 && 可持久化trie
字典树 简介:字典树,又称单词查找树,Trie树,是一种树形结构,是哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较. 性质:根节点不包含字符,除根节点外每一个 ...
- Eclipse项目左侧调整显示或者不显示路径
View Menu(倒三角)-->Package Presentation-->选择Flat或Hierarchical
- 雇佣K个工人的最小费用 Minimum Cost to Hire K Workers
2018-10-06 20:17:30 问题描述: 问题求解: 问题规模是10000,已经基本说明是O(nlogn)复杂度的算法,这个复杂度最常见的就是排序算法了,本题确实是使用排序算法来进行进行求解 ...