hdu1025 最大上升字串
Constructing Roads In JGShining's Kingdom
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22338 Accepted Submission(s): 6348
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. ^_^
You should tell JGShining what's the maximal number of road(s) can be built.
1 2
2 1
3
1 2
2 3
3 1
My king, at most 1 road can be built.
Case 2:
My king, at most 2 roads can be built.
Huge input, scanf is recommended.
#include <iostream>
#include <stdio.h>
#define N 500003
using namespace std;
int road[N];
int dp[N];
const int inf = 0x7fffffff;
int main(){
int c=;
int n;
while((scanf("%d",&n))!=EOF){
for(int i=;i<=n;i++){
dp[i]=inf;
}
for(int i=;i<n;i++){
int tmp1,tmp2;
scanf("%d%d",&tmp1,&tmp2);
road[tmp1]=tmp2;
}
int len=;
for(int i=;i<=n;i++){
*lower_bound(dp,dp+n,road[i])=road[i];
}
len = lower_bound(dp,dp+n,inf)-dp;
if(len==)
{
cout<<"Case "<<c++<<":"<<endl;
cout<<"My king, at most 1 road can be built."<<endl;
}
else
{
cout<<"Case "<<c++<<":"<<endl;
cout<<"My king, at most "<<len<<" roads can be built."<<endl;
}
cout<<endl;
}
return ;
}
用STL lower_bound简便到计算出dp序列中,大于等于road[i]到最小到指针,然后替换之,这样dp【i】保存的是第i最大的数。
最后计算dp中非inf到数字到个数,就可以得到road序列中最长递增子序列。
hdu1025 最大上升字串的更多相关文章
- 最大公共字串LCS问题(阿里巴巴)
给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...
- 编程:使用递归方式判断某个字串是否回文(Palindrome)
Answer: import java.util.Scanner; public class Palindrome { private static int len;//全局变量整型数据 privat ...
- NOIP2002字串变换[BFS]
题目描述 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ 可以变换为 B1$.A2 ...
- 字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。
split() 方法将字符串分割为字符串数组,并返回此数组. stringObject.split(separator,limit) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...
- mormot 数据集转换为JSON字串
mormot 数据集转换为JSON字串 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graph ...
- 字串变换(codevs 1099)
题目描述 Description 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ ...
- @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不
@有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中 2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不加@那么需要用一些转义符\来显示一些特 ...
- PHP 中替换若干字符串字串为数组中的值,不用循环,非常高效
替换某个字符串中的一个或若干个字串为数组中某些值 php本身有自带的函数,可以不用循环非常高效的实现其效果: 实例代码: $phrase = "You should eat fruit ...
- [FAQ]String(字串相連)與StringBuilder的差別、原理與優缺點?
原文位於 http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/09/msdn_string_stringbuilder.aspx [FAQ]St ...
随机推荐
- 读取bq26500电池电量 Driver
bq26500是用于锂离子电池.锂聚合物电池充电.放电控制和监控的集成电路芯片.利用它能够准确提供电池充电.放电.电池温度.充放电电压.电池电量等相关数据.不须要外接微处理器參与电池充电.放电等相关数 ...
- 爬虫之多线程 多进程 自定义异步IO框架
什么是进程? 进程是程序运行的实例,是系统进行资源分配和调度的一个独立单位,它包括独立的地址空间,资源以及1个或多个线程. 什么是线程? 线程可以看成是轻量级的进程,是CPU调度和分派的基本单位. 进 ...
- 纯CSS实现多行文字垂直居中几种方法解析
场景:父元素 高度固定,如何使其中的文字垂直居中? 1.table布局: 利用display:table+display:table-cell的方法 <!DOCTYPE html> < ...
- <input type = "submit"> 提交方式和用js的form.submit()有什么区别?
假设: A表单内有<input type="submit">,通过点击这个input来提交表单 B表单内没有<input type="submit&qu ...
- 黑马程序员_7K月薪面试题之_交通灯管理系统
----------- android培训.java培训.java学习型技术博客.期待与您交流! --------- 1,交通灯管理系统的项目需求 模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: ...
- 解决:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile
在项目构建的时候遇到了这样的问题:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile(d ...
- 转: Android中的签名机制
转载请注明出处:http://www.blogjava.net/zh-weir/archive/2011/07/19/354663.html Android APK 签名比对 发布过Android应用 ...
- TestNG 七 annotation
TestNG中用到的annotation的快速预览及其属性. @BeforeSuite: 被注释的方法将在所有测试运行前运行 @AfterSuite: 被注释的方法将在所有测试运行后运行 @Be ...
- 基于Prometheus搭建SpringCloud全方位立体监控体系
前提 最近公司在联合运维做一套全方位监控的系统,应用集群的技术栈是SpringCloud体系.虽然本人没有参与具体基础架构的研发,但是从应用引入的包和一些资料的查阅大致推算出具体的实现方案,这里做一次 ...
- RelativeLayout与LinearLayout的比较
转自:http://blog.csdn.net/onepiece2/article/details/26396287 RelativeLayout 是相对布局在页面上相对于页面坐标进行布局设置.比如可 ...