您的位置:首页 > 其它

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.

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: