1. 程式人生 > >單調棧經典模板(Codeforces Round #305 (Div. 2), problem: (D) Mike and Feet)

單調棧經典模板(Codeforces Round #305 (Div. 2), problem: (D) Mike and Feet)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
stack<int> stk;
int a[maxn] , dpl[maxn] , dpr[maxn] , ans[maxn];
int main() {
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
    for(int i = 1; i <= n; i++) 
        while(!stk.empty() && a[stk.top()] >= a[i]) stk.pop();
        if(stk.empty()) dpl[i] = 1;
        else dpl[i] = stk.top()+1;
        stk.push(i);
    }
    while(!stk.empty()) stk.pop();
    for(int i = n; i >= 1; i--) {
        while(!stk.empty() && a[stk.top()] >= a[i]) stk.pop();
        if(stk.empty()) dpr[i] = n;
        else dpr[i] = stk.top()-1;
        stk.push(i);
    }
    /*memset(ans , 0 , sizeof(int)*(n+1));
    for(int i = 1; i <= n; i++) ans[dpr[i]-dpl[i]+1] = max(ans[dpr[i]-dpl[i]+1] , a[i]);
    for(int i = n-1; i >= 1; i--) ans[i] = max(ans[i] , ans[i+1]);
    for(int i = 1; i <= n; i++) printf("%d%c",ans[i]," \n"[i == n]);*/
    return 0;
}


相關推薦

單調經典模板(Codeforces Round #305 (Div. 2), problem: (D) Mike and Feet)

#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 10; stack<int> stk; int a[maxn] , dpl[maxn] , dpr[max

Codeforces Round #510 (Div. 2), problem (D) Petya and Array 分治

本題如果採用兩層for迴圈遍歷每個l和r,時間複雜度為O(n2),將會超時。所以可以採用分治的演算法,將陣列二分遞迴,完成一個分支之後將此次l和r之間的數進行排序,目的是能以O(n)的時間複雜度統計出此分支符合要求的個數。 ac程式碼: #include <bits/std

Codeforces Round #365 (Div. 2) Problem D.Mishka and Interesting sum 解題報告

題目連結:Here! 題目大意:給你n個數,然後m次查詢,每一次查詢輸出所有在給定區間內出現偶數次數的數的異或值,如果只有一個出現偶數次數的數,則直接輸出該數,如果沒有出現偶數次數的數,則輸出0。 解題思路:看到區間查詢,第一反應是線段樹,但是用線段樹來統計區間內出現次數為

Codeforces Round #436 (Div. 2), problem: (D) Make a Permutation!

scanf problem codeforce ++ round class span com make #include <cstdio> #include <cstring> #include <vector> using names

Codeforces Round #517 (Div. 2), problem (D) Minimum path 貪心

本題運用貪心演算法,需要按照字典序輸出路徑,路徑包含2n-1個字元,只需要按正序求出當前情況下的最優解即可。需要注意的是當最優解相同的時候需要保留剩餘更改次數更大的那個路徑。 在求解本題的過程中由於構思的偏差出現了Runtime error、Memory limit exceeded

Codeforces Round #228 (Div. 2), problem: (A) Fox and Number Game

Note In the first example the optimal way is to do the assignment: x2 = x2 - x1. In the second example the optimal sequence of operations is: x3 = x3 -

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前綴和

題目 scan 開心 color 一個 lin cond cati ict The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordi

Codeforces Round #435 (Div. 2) c+d

pan define ons eve vector class bits ace mod C:給n和k要求,找出n個不同的數,使得亦或起來等於k 可以先預處理從1到1e5,找亦或起來等於(11111111111111111)(二進制)的所有對數,然後四個一起亦或就是0了,

Codeforces Round #436 (Div. 2), problem: (C) Bus

cst lag () turn code span cpp urn print #include <cstdio> int main(){ long long a,b,f,k,i,tot,s[11000],cnt; scanf("%lld%lld%lld%l

Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 組合數學

note als 9.png fig fine 連接 time mod reac — This is not playing but duty as allies of justice, Nii-chan! — Not allies but j

Codeforces Round #451 Div. 2 C D E

eve == ont span har else const 實用 iterator C。Phone Numbers 之前沒有做過字典樹……感覺這個題字典樹也能做……就拿來練一練字典樹……板子好多地方寫的都不夠好,還需要繼續改…… emmm這個……卡了好久啊……不過好在還

Codeforces Round #457 (Div. 2) A】 Jamie and Alarm Snooze

als lar clu efi out .com bit source fin 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 暴力往前走x分鐘就好。 直到出現7為止。 【代碼】 #include <bits/stdc++.h&

Codeforces Round #457 (Div. 2) C】Jamie and Interesting Graph

include pac bits namespace mark span .com ifd graph 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 找比n-1大的最小的素數x 1-2,2-3..(n-2)-(n-1)長度都為1 然後(

Codeforces Round #341 (Div. 2) E - Wet Shark and Blocks

取出 splay names mem pri 思路 turn const gpo 題目大意:有m (m<=1e9) 個相同的塊,每個塊裏邊有n個數,每個數的範圍是1-9,從每個塊裏邊取出來一個數組成一個數,讓你求組成的方案中 被x取模後,值為k的方案數。(1<=

Codeforces Round #252 (Div. 2)-C,D

++ con cli 排列 tor 節點 -c fin ios C題就是一個簡單的模擬。首先給每一個人兩個。然後把剩下的都給一個人就好了。 給的時候蛇形給。

Codeforces Round #482 (Div. 2) C】Kuro and Walking Route

return lse lac and ios inf == efi div 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 把x..y這條路徑上的點標記一下。 然後從x開始dfs,要求不能走到那些標記過的點上。記錄節點個數為cnt1(包括

Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun

reg abs ins getchar stream 直線 name stdout etc 因為是x,y均為整數因此對於同一直線的點,其最簡分數x/y是相同的(y可以為0,這裏不做除法)於是將這些點不斷求最簡分數用pair在set中去重即可。 #include <

Codeforces Round #511 (Div. 2), problem: (C) Enlarge GCD 數論

思路大體是先找到an取值範圍內的質數並儲存,求出輸入的資料中最大公倍數並將a陣列都除以最大公倍數,然後遍歷an的取值範圍(從2開始,1不算),如果是質數就統計這個質數和它的倍數,就是使最小公倍數增加所需要去掉的數的個數,遍歷過程中取最小值即可。並注意判定特殊情況。這個方法幾乎要超時了似乎

Codeforces Round #512 (Div. 2)B.Vasya and Cornfield

B. Vasya and Cornfield time limit per test 1 second memory limit per test 256 megabytes

Codeforces Round #514 (Div. 2), problem: (A) Cashier

給定客人來的時間和要接待他們的時間,然後算出自己能夠有多少次的休息 挨個列舉每一段的空閒時間就好啦,第i段的空閒時間是 t[i] - t[i-1] - last[i-1],然後除以a求和即可。 程式碼如下: #include <bits/stdc++.h>