标签:codeforces 91b queue 单调栈 acm 数据结构
Description
There are n walruses standing in a queue in an airport. They are numbered starting from the queue‘s tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there‘s a younger walrus standing in front of him, that is, if exists such j (i?<?j), that ai?>?aj. Thedispleasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than thei-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2?≤?n?≤?105) — the number of walruses in the queue. The second line contains integers ai (1?≤?ai?≤?109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to bestrictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus‘s displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Sample Input
6 10 8 5 3 50 45
2 1 0 -1 0 -1
7 10 4 6 3 2 8 15
4 2 1 0 -1 -1 -1
5 10 3 1 10 11
1 0 -1 -1 -1
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; typedef long long ll; const int MAXN = 1e5 + 10; struct W { int age, pos, right; bool operator<(const W& rhs)const { if(age != rhs.age)return age < rhs.age; return pos < rhs.pos; } }; W w[MAXN]; int disp[MAXN]; int main() { int n; while(scanf( "%d", &n ) == 1) { for(int i = 1; i <= n; i++) { scanf( "%d", &w[i].age ); w[i].pos = w[i].right = i; } sort( w + 1, w + n + 1 ); disp[w[1].pos] = - 1; for(int i = 2; i <= n; i++) { if(w[i - 1].right > w[i].pos) { w[i].right = w[i - 1].right; } disp[w[i].pos] = w[i].right - w[i].pos - 1; } for(int i = 1; i < n; i++) printf( "%d ", disp[i] ); printf( "%d\n", disp[n] ); } return 0; }
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #define MAX 100002 using namespace std; int a[MAX],ans[MAX]; vector<int> v,num; int main() { int n; //freopen("data.txt","r",stdin); while(~scanf("%d",&n)){ for(int i=0;i<n;i++) scanf("%d",&a[i]); v.clear(); num.clear(); for(int i=n-1;i>=0;i--){ if(v.size()==0 || v.back()>=a[i]){ v.push_back(a[i]); num.push_back(i); ans[i]=-1; }else{ int j = (lower_bound(v.rbegin(),v.rend(),a[i]) - v.rbegin()); j = (int)v.size() - j - 1; ans[i] = num[j+1] - i - 1; } } for(int i=0;i<n;i++){ if(i) printf(" "); printf("%d",ans[i]); } printf("\n"); } return 0; }
标签:codeforces 91b queue 单调栈 acm 数据结构
原文地址:http://blog.csdn.net/maxichu/article/details/46313919