Codeforces Round #305 (Div. 2) D. Mike and Feet (单调栈)
2017-11-23 21:03
495 查看
题意:
给你n个数字,求出每1个,2个,3个…n个连续数字所构成的区间里面的最小值之中的最大值(好绕口,解释下样例好了)10
1 2 3 4 5 4 3 2 1 6
当以3个连续数字作为一个区间的时候,我们可以得到每个区间的最小值是1,2,3,4,3,2,1,1所以输出的第三个数字是4
思路:
利用单调栈,可以求出对于某个数字来说,到左边或到右边第一个比它小的数字的距离,那么如果我们求出这个距离以后,按照他们从大到小的排序,因为要求的值是最大值,那么就可以根据这个这个距离o(n)地更新出答案错误及反思:
代码:
#include<bits/stdc++.h> using namespace std; const int maxn = 210000; struct poi { int val; int l,r; }arr[maxn]; int n,ans[maxn]; bool cmp(poi a,poi b) { return a.val>b.val; } int main() { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&arr[i].val); stack<int> s; for(int i=0;i<n;i++) { if(s.empty()||arr[s.top()].val<=arr[i].val) s.push(i); else { while(!s.empty()&&arr[s.top()].val>arr[i].val) { int temp=s.top(); arr[temp].r=i-temp-1; s.pop(); } s.push(i); } } while< 4000 /span>(!s.empty()) { int temp=s.top(); arr[temp].r=n-temp-1; s.pop(); } for(int i=n-1;i>=0;i--) { if(s.empty()||arr[s.top()].val<=arr[i].val) s.push(i); else { while(!s.empty()&&arr[s.top()].val>arr[i].val) { int temp=s.top(); arr[temp].l=temp-i-1; s.pop(); } s.push(i); } } while(!s.empty()) { int temp=s.top(); arr[temp].l=temp; s.pop(); } sort(arr,arr+n,cmp); int now=0; for(int i=0;i<n;i++) { for(int j=now;j<=arr[i].r+arr[i].l;j++) ans[now++]=arr[i].val; } for(int i=0;i<n;i++) { if(i) printf(" %d",ans[i]); else printf("%d",ans[i]); } puts(""); }
相关文章推荐
- Codeforces #305 div2 D. Mike and Feet 递推/单调栈
- 单调栈经典模板(Codeforces Round #305 (Div. 2), problem: (D) Mike and Feet)
- Codeforces Round #305 (Div. 2)D---Mike and Feet(单调栈)
- CodeForces Round #305 (div1) B. Mike and Feet (单调栈)
- Codeforces Round #305 (Div. 1) B. Mike and Feet(单调栈)
- Codeforces Round #305 (Div. 1) B. Mike and Feet(单调栈)
- Codeforces Round #305 (Div. 1)-B. Mike and Feet(单调栈)
- Codeforces Round #305 Mike and Feet(单调栈)
- Codeforces Round #305 (Div. 1) B. Mike and Feet(并查集)
- Codeforces Round #305 (Div. 1)B - Mike and Feet 线段树
- codeforces/#305 Div1/547/B Mike and Feet
- Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈
- cf#305-D. Mike and Feet- 单调栈/ (线段树上二分)
- Codeforces Round #305 (Div. 2) D.Mike and Feet
- Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈
- CF 305 div2 D. Mike and Feet (递推)
- Mike and Feet(单调栈)
- codeforces #305 547B B. Mike and Feet(单调栈)
- Codeforces 547B - Mike and Feet (单调栈)
- codeforces 548 D. Mike and Feet (单调栈)