1. 程式人生 > >hdu1025 最長不下降子序列nlogn演算法

hdu1025 最長不下降子序列nlogn演算法

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 下降序列nlogn演算法

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

下降序列nlogn算法詳解

利用 pos 要求 它的 子序列 解決 post 第一個 就是 今天花了很長時間終於弄懂了這個算法……畢竟找一個好的講解真的太難了,所以勵誌我要自己寫一個好的講解QAQ 這篇文章是在懂了這個問題n^2解決方案的基礎上學習。

下降序列nlogn

b[i]表示長度為i的最長不下降子序列的最小末尾元素的值顯然它是單調遞增的,滿足二分性質,然後就可以愉快地二分啦. #include<iostream> #include<cstdio> #include<queue> #include<algorithm&g

HDU 5532 Almost Sorted Array(下降序列nlogn模板)

We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But s

O(n log n)求上升序列下降序列

clas 每一個 for spa pen pan close color style 考慮dp(i)表示新上升子序列第i位數值的最小值.由於dp數組是單調的,所以對於每一個數,我們可以二分出它在dp數組中的位置,然後更新就可以了,最終的答案就是dp數組中第一個出現正無窮的位

SSl 2756_獨立集_下降序列

pri == clas stdio.h 我們 心情 [] 題目 can 題目描述 有一天,一個名叫順旺基的程序員從石頭裏誕生了。又有一天,他學會了冒泡排序和獨立集。在一個圖裏,獨立集就是一個點集,滿足任意兩個點之間沒有邊。於是他就想把這兩個東西結合在一起。眾所周知,獨立集是

luogu2766 下降序列問題

nic eof 什麽 () num can cpp for 分層圖 第一問DP水過。dp[i]代表以i結尾的最長不下降子序列長度。 二三問網絡流。 第二問是說每個子序列不能重復使用某個數字。 把每個點拆成p(i),q(i)。連邊。 要是dp[i]=1,連源,p(i) 要是d

【寧波市第23屆中小學生計算機程序設計競賽(初中組)T3】馬(排序,下降序列

決定 pri 需要 程序設計 分開 組成 最長 只有一個 應該 題目描述 戰神阿瑞斯聽說2008年在中華大地上,將舉行一屆規模盛大的奧林匹克運動會,心中頓覺異常興奮,他召集了n位天將,騎著他們自己的天馬,準備在廣闊的天空上,舉行一場空前的天馬天將列隊表演。天馬在熟悉了自己的

「網絡流24題」下降序列問題

計算 二分圖匹配 原理 最長 span 情況 遞增 mic ros 傳送門:>Here< 題意: 給定正整數序列$x_1,...,x_n$ (1)計算其最長不下降子序列的長度s。 (2)計算從給定的序列中最多可取出多少個長度為s的不下降子序列。 (

大連續序列和/下降序列/公共序列/迴文

//最大連續子序列和 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 10010; int A[maxn],dp[maxn]; int main(){ int

codeup21280:下降序列問題(LIS:Longest Increasing Sequence---dp基礎題)

題目地址:http://codeup.cn/problem.php?id=21280&csrf=BoAHUd12vsqOUBpidoqhiueWMmKAEEdM 21280: 最長上升子序列 題目描述 一個數列ai如果滿足條件a1 < a2&nb

HDU 1160 FatMouse's Speed(動態規劃 LIS下降序列

題目連結: #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cstdlib> #include <map>

程式設計基礎33 下降序列

1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in

P2766 下降序列問題 題解(網路流)

題目連結 最長不下降子序列問題 解題思路 分成三小問解決。 第一小問,求\(LIS\),因為\(n<=500\),直接\(O(N^2)\)暴力求解即可。 第二三小問,建立模型用網路流求解。 對於第二小問 \((1)\)首先,因為每個點只能使用一次,考慮拆點,把每一個點拆成\(i,n+i\)兩個點,

ACM訓練 身高排隊、導彈攔截 [下降序列序列序列小覆蓋]

題目 題目分析 整體程式碼 相似問題 題目 題目描述 若干人排成一行,且身高分別為b1,b2,…,bn。準備從中選出一組滿足身高不降的人組成一隊。 例如13,7,9,16,3

poj 3670Eating Together(上升下降序列

Description The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently number

P2766 下降序列問題

就是 如果 flow 輸入格式 rom hang chan names truct \(\color{#0066ff}{題目描述}\) ?問題描述: 給定正整數序列x1,...,xn 。 (1)計算其最長不下降子序列的長度s。 (2)計算從給定的序列中最多可取出多少個長度為

【模板】下降序列

#include <cstdio> using namespace std; int n; int a[MAXN]; int lis[MAXN]; // 第1個數至第i個數中的最長不下降子序列 int main() { scanf("%d", &n

洛谷2766下降序列問題

題目連結:最長不下降子序列問題 這一個問題雖然有三小問,但是每一個小問題的連線非常緊密 對於第一問,直接\(O(n^2)\)水過,你要用\(O(nlogn)\)當然也可以啊 二三問考慮使用網路流求解 我們利用第一問中得到的dp關係來建圖: 很明顯的是這裡的每一個數只能用一次,所以我們將每一個點拆成兩

PAT甲級1045 Favorite Color Stripe(下降序列

1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite