1. 程式人生 > >Painting Fence[分治]

Painting Fence[分治]

**題意:**n個寬度為1,高度為ai的板子,橫豎一筆刷漆,問最小需要多少筆刷完。
思路:貪心加分治,首先對於一些[l, r]序號區間內的板子,豎著刷為(r-l+1)花費,橫著刷的話肯定要先刷到最小的長度的那個,然後分成左右兩半,然後分治重複遞迴。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<algorithm>
#include<map>
#include<set>
#include<queue> #include<vector> #include<cstdlib> #include<list> #include<stack> #include<cmath> #include<iomanip> using namespace std; //#pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; void debug() {cout << "ok running!" << endl;} int
n, a[5005]; int solve(int l, int r) { if(l == r) return a[l]!=0; if(l > r) return 0; //cout << l << " " << r << endl; int m = 0x3f3f3f3f, pos = -1; int res = (r-l+1); for(int i = l; i <= r; ++i) { if(a[i] < m) { m = a[i]; pos = i; } } for
(int i = l; i <= r; ++i) a[i] -= m; return min(res, solve(l, pos-1) + solve(pos+1, r) + m); } int main() { ios::sync_with_stdio(false); #ifndef ONLINE_JUDGE //freopen("input.txt", "r", stdin); #endif // ONLINE_JUDGE while(cin >> n) { for(int i = 0; i < n; ++i) { cin >> a[i]; } int ans = solve(0, n-1); cout << ans << endl; } return 0; }

相關推薦

Painting Fence[分治]

**題意:**n個寬度為1,高度為ai的板子,橫豎一筆刷漆,問最小需要多少筆刷完。 思路:貪心加分治,首先對於一些[l, r]序號區間內的板子,豎著刷為(r-l+1)花費,橫著刷的話肯定要先刷到最小的

Codeforces 448C Painting Fence分治

get ret define fin 所有 技術 tdi 如何 這一 題目鏈接:http://codeforces.com/problemset/problem/448/C 題意:   有n個木板豎著插成一排柵欄,第i塊木板高度為a[i]。   你現在要將柵欄上所有地

C - Painting Fence分治

題意:給出寬為1高為Ai的木板n條,排成一排,每次上色只能是連續的橫或豎並且寬度為1,問最少刷多少次可以使這些木板都上上色 分析:刷的第一步要麼是所有的都豎著塗完,要麼是先橫著把最矮的塗完,如果是第一種,那麼ans等於n,如果是第二種,那麼ans=最矮的高度+被刷掉最矮的後,新的幾段不連續木板最小上色次數,

#256 (Div. 2)C. Painting Fence

using paint its fence int () scanf 我們 include 題意:給出籬笆的高度,寬度都是1,我們用寬度為1的刷子刷,只能橫著或者豎著刷,連續的,問最少多少次全刷完 思路:我們可以全部豎著刷,N次,然後我們如果橫著的話就是連續的非0數列中最短

Codeforces 448C. Painting Fence

ole per con surf name stream splay better closed Bizon the Champion isn‘t just attentive, he also is very hardworking. Bizon the Champio

[Codeforces 448C]Painting Fence

des ade mpio sta them orange stroke can sam Description Bizon the Champion isn‘t just attentive, he also is very hardworking. Bizon the

Codeforces 448C Painting Fence(分治法)

劃分 .com 規劃 == sum tps codeforce nbsp 長度 題目鏈接:http://codeforces.com/contest/448/problem/C 題目大意:n個1* a [ i ] 的木板,把他們立起來,變成每個木板寬為1長為 a [ i

Codeforces 448C:Painting Fence 刷柵欄 超級好玩的一道題目

Note In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the plank

Painting Fence

getch using show 文件 new blank har pan 註意   傳送門 Descriptionzed 最近總是受到 Farmer 的困擾,因此他在自家的門前插了一排柵欄以防農氣的入侵。柵欄由 N 個豎條柵欄橫向組成,每個豎條柵欄寬度為 1。過了一段時

【題解】Painting Fence

tin get line color template clas code temp endif 【題解】Painting Fence 分治模板。貪心加分治。直接\(O(n^2logn)\)分治過去。考慮一塊聯通的柱形是子問題的,是遞歸的,貪心分治就可。記得對\(r-l+1

洛谷 P2205 [USACO13JAN]畫柵欄Painting the Fence

%d lan rest target write call logic www. 離散 P2205 [USACO13JAN]畫柵欄Painting the Fence 題目描述 Farmer John has devised a bril

[luogu P2205] [USACO13JAN]畫柵欄Painting the Fence

radi update usaco node nod hide pri click get [luogu P2205] [USACO13JAN]畫柵欄Painting the Fence 題目描述 Farmer John has devised a brilliant

SPOJ:House Fence分治&DP)

day 一個 prev 現在 wan stroke AS make ica "Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his college.

[USACO13JAN]畫柵欄Painting the Fence 洛谷p2205

題目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number line). He

[USACO13JAN]畫柵欄Painting the Fence 洛谷p2205

題目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number

Gym - 101911E Painting the Fence set

There is a beautiful fence near Monocarp's house. The fence consists of nn planks numbered from left to right. The ii-th plank has colo

CodeForces-1132C Painting the Fence

decide force ast nes 查詢 res side 最大值 去掉 題目鏈接 https://vjudge.net/problem/CodeForces-1132C 題面 Description You have a long fence which consi

【Codeforces 1132C】Painting the Fence

def 覆蓋 前綴 連續 前綴和 每一個 順序 第一個 輸出 Codeforces 1132 C 題意:給一些區間\([l_i,r_i]\),從中刪掉兩個,求剩下的區間最多能夠覆蓋的格子數量。 思路:首先枚舉第一個刪掉的區間,然後我們可以通過差分來求出每個格子被多少個區間覆

BZOJ3163&Codevs1886: [Heoi2013]Eden的新背包問題[分治優化dp]

一行 data gis table 一個 ans 進制 玩偶 printf 3163: [Heoi2013]Eden的新背包問題 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 428 Solved: 277[Subm

分治】簡單說說快排

ostream stdout ++i rand() oid fclose cnblogs clu 快排 說到快拍,大家都會首先想到sort函數這個神奇的東西 但是,我們總得知道快拍主要用的分治思想 所以就說一說快拍吧 首先是分類 快拍主要有三種方式: 一、以第一個數為基準排