1. 程式人生 > >hdu1025最長上升子序列 (加二分優化)

hdu1025最長上升子序列 (加二分優化)

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. ^_^

相關推薦

hdu1025上升序列 二分優化

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 resou

Prince and Princess—上升序列dp+二分查詢

題目連結:Prince and Princess UVA - 10635  題目大意: 給出n, m, k,求兩個長度分別為m + 1 和 k + 1且由1~n * n組成的序列的最長公共子序列長的。 解題思路: 按一般的o(n^2)的演算法超時了,所以

LeetCode 300. 上升序列Longest Increasing Subsequence

輸出 pan 因此 需要 ack 時間復雜度 family class 一個數 題目描述 給出一個無序的整形數組,找到最長上升子序列的長度。 例如, 給出 [10, 9, 2, 5, 3, 7, 101, 18], 最長的上升子序列是 [2, 3, 7, 101],

BZOJ3591 上升序列狀壓dp

[] line 以及 char == 最長上升子序列 lis stdin lib   之前聽說過一種dp套dp的trick,大致是用另一個dp過程中用到的一些東西作為該dp的狀態。這個題有異曲同工之妙。   考慮求LIS時用到的單調隊列。設f[S]為所選取集合為S的方案數,

上升序列兩種方法

常規方法:(n^2) #include<iostream> using namespace std; int i,j,n,a[100],b[100],max; int main() { cin>>n; for(i=0;i<n;i++)

上升序列Longest increasing subsequence

問題描述         對於一串數A={a1a2a3…an},它的子序列為S={s1s2s3…sn},滿足{s1<s2<s3<…<sm}。求A的最長子序列的長度。 動態規

用樹狀數組寫的上升序列友好城市,nlogn。

log stream blog sum spa 最長上升子序列 () += def #include<iostream> #include<algorithm> #define maxn 100000 #define lb(x) x&-x

【BZOJ】3173: [Tjoi2013]上升序列樹狀數組

nss 貢獻 isp 轉化 復雜 src printf efi col 【題意】給定ai,將1~n從小到大插入到第ai個數字之後,求每次插入後的LIS長度。 【算法】樹狀數組||平衡樹 【題解】 這是樹狀數組的一個用法:O(n log n)尋找前綴和為k的最小位置。(當數列

上升序列dp

tps ann test case std intro lar ont sta into 鏈接:https://www.nowcoder.com/questionTerminal/d83721575bd4418eae76c916483493de來源:牛客網廣場上站著一支隊伍

1134 上升序列 序列型 DP

col 直接 般的 圖片 時間復雜度 方法 第一個 def 但是 思路: 由於一般的動態規劃時間復雜度是O(n^2)(哈哈哈哈 第一次用的就是這個!)用在這裏由於n最大為50000 所以會超時 到這裏我們可以用一個數組來動態維護這個最長上升的子序列,將你要輸入的子序列一個

18.10.9 不好做的上升序列nlogn樹狀數組解LIS

algo eve void operator ++ 做的 pre fin pro 描述 一個數的序列bi,當b1 < b2 < ... < bS的時候,我們稱這個序列是上升的。對於給定的一個序列(a1, a2, ..., aN),我們可以得到一些上升的子

BZOJ - 5427:上升序列 二分&思維

現在給你一個長度為n的整數序列,其中有一些數已經模糊不清了,現在請你任意確定這些整數的值, 使得最長上升子序列最長。(為何最長呢?因為hxy向來對自己的rp很有信心)   Input 第一行一個正整數n 接下來n行第i行格式如下

OpenJ_Bailian - 2757 上升序列O(n2)演算法和O(nlogn)演算法

一個數的序列  bi,當  b1 <  b2 < ... <  bS的時候,我們稱這個序列是上升的。對於給定的一個序列(  a1,  a2, ...,  aN),我們可以得

【bzoj5427】上升序列貪心+LIS

貪心 去掉 ati lse com set event color tps   題目傳送門:https://www.lydsy.com/JudgeOnline/problem.php?id=5427   因為noip,博客咕了好久,這幾天集中填一下坑。   這題我們可以

初入DP:上升序列LIS

Description 我們都知道上體育課時,體育老師會讓我們按身高從小到大(或從大到小)排成一排。可是近日體育老師周老師卻有點煩心, 他教的班級來了幾個插班生,可他們有的不守規矩,沒有按照身高大小來插入隊伍,導致隊伍很難看。現在周老師有一個問 題:給定一排人的身高,問能否至多去掉一個人,使

Leetcode 300:上升序列詳細的解法!!!

給定一個無序的整數陣列,找到其中最長上升子序列的長度。 示例: 輸入: [10,9,2,5,3,7,101,18] 輸出: 4 解釋: 最長的上升子序列是 [2,3,7,101],它的長度是 4。

上升序列動態規劃,n²

package 實驗三; public class 最長上升子序列 { public static void main(String[] args) { E e=new E(); e.way(); e.show1(); e.show2(); e.sh

LIS 上升序列 n*logn 模板 二分查詢+遞迴

      最長上升子序列是很早就接觸了的問題了,一直用的是動態規劃n*n的方法,也知道那不是最好的,可以優化,今天看部落格無意中看到LIS,LCS兩個詞,就特意找了部落格看了看,主要是理解一下這裡的思想,其實蠻複雜難懂的,自己很難說清楚,還是得引用人家的部落格才行。點選開

動態規劃 上升序列LIS

O(N2)寫法: memset(dp, 0, sizeof(dp)) for(i = 0; i < n; i++) {          dp[i]= 1;          for(j= 0; j < i; j++) {                   

上升序列LIS長度 Onlogn演算法 hdu1950為例

最長上升子序列 最長上升子序列(Longest Increasing Subsequence,LIS),是指一個序列中最長的單調遞增的子序列。 該問題有一個n2的動態規劃解法,這裡介紹O(nlogn)的解法。 設a[]是原序列,d[i]表示長度為i的上升子