Codeforces Beta Round #75 (Div. 2 Only)——D
2013-04-16 22:27
465 查看
自己的代码1937MS险过,就不拿出来丢人了。感觉这份代码思路不错,拿出来分享一下。
D. Queue
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 the i-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 be strictly 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.
D. Queue
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 the i-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 be strictly 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.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn=100000+5; struct node { int age,pos,mark; }q[maxn]; int res[maxn]; bool cmp(node x,node y) { if(x.age!=y.age) return x.age<y.age; return x.pos<y.pos; } int main() { int n,d; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&d); q[i]=(node){d,i,i}; } sort(q,q+n,cmp); memset(res,-1,sizeof(res)); for(int i=1;i<n;i++) { if(q[i].pos<q[i-1].mark)///因为存在覆盖关系,mark表示age小于它且位置最右边的点的实际位置 q[i].mark=q[i-1].mark;///同样的将mark更新,使得计算i时只需要借用i-1的mark res[q[i].pos]=q[i].mark-q[i].pos-1; } for(int i=0;i<n;i++) printf("%d%c",res[i],i==n-1?'\n':' '); return 0; }
相关文章推荐
- Codeforces Beta Round #76 (Div. 1 Only)
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分
- Codeforces Beta Round #16 (Div. 2 Only), problem: (E) Fish 概率DP
- Codeforces Beta Round #77 (Div. 2 Only)——A,B,C
- 【预处理】Codeforces Beta Round #80 (Div. 1 Only) ——Time to Raid Cowavans
- Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present 记忆化搜索
- Codeforces Beta Round #83 (Div. 1 Only) E.Darts 凸多边形面积交
- Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp
- Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland
- Codeforces Beta Round #87 (Div. 1 Only)
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- 【CS Round #48 (Div. 2 only)】Water Volume
- 【CS Round #37 (Div. 2 only) A】Boring Number
- Codeforces Beta Round #6 (Div. 2 Only) E. Exposition
- Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array
- Codeforces Round #447 (Div 2 Only) Hack+FST记
- Codeforces Beta Round #25 (Div. 2 Only) - E. Test (KMP)
- CF # 92 (Div. 2 Only)
- Codeforces Beta Round #18 (Div. 2 Only)——B
- Codeforces Beta Round #87 (Div. 1 Only)B Lawnmower(贪心)