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 ...
随机推荐
- linux xfs文件系统无法用readdir获取dirent文件类型d_type则用stat获取暨stat函数讲解
stat函数讲解 表头文件: #include <sys/stat.h> #include <unistd.h>定义函数: int stat ...
- PhpStorm 对 AngularJS 的支持
非常喜爱用AngularJS来构建web应用程序的前端吗? PhpStorm 使得在其上进行 AngularJS 相关的工作同其它得到IDE支持的编程语言的工作一样容易! AD:51CTO首届中国AP ...
- 基于CentOS与VmwareStation10搭建hadoop环境
基于CentOS与VmwareStation10搭建hadoop环境 目 录 1. 概述.... 1 1.1. 软件准备.... 1 1.2. 硬件准备.... 1 2. 安装与配置虚拟机.. ...
- asp.net 大文上传配置
配置iis的文件上传大小控制: 今天先说一个问题,在window server2008 下配置上传文件的大小,除了工程自身的config外还需要配置一下服务器上的applicationhost.con ...
- Kolla 4.0.0环境下VIP无法迁移问题排查
VRRP无法切换VIP的问题分析: Keepalived的配置文件:/etc/kolla/keepalived/keepalived.conf当中,nopreempt选项是影响切换的因素之一,另一个因 ...
- hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- unity3d格式的导出与加载
http://blog.csdn.net/nateyang/article/details/7567831 1.导出.unity3d格式资源: http://game.ceeger.com/Scrip ...
- 算法笔记_098:蓝桥杯练习 算法提高 盾神与条状项链(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 有一天,盾神捡到了好多好多五颜六色的珠子!他心想这些珠子这么漂亮,可以做成一条项链然后送给他心仪的女生~于是他用其中一些珠子做成了长度为n ...
- 对象的序列化(Serialization)
一.什么是序列化 序列化表示将一个对象转换成可存储或可传输的状态,序列化后对象可以在网络上进行传输,也可以存储到本地.对象的寿命通常随着生成该对象的程序的终止而终止.有时候,可能需要将对象的状态保存下 ...
- angularJS使用内置服务
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...