解题报告 之 CodeForces 91B Queue

解题报告 之 CodeForces 91B Queue

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

Input

6
10 8 5 3 50 45

Output

2 1 0 -1 0 -1 

Input

7
10 4 6 3 2 8 15

Output

4 2 1 0 -1 -1 -1 

Input

5
10 3 1 10 11

Output

1 0 -1 -1 -1 

题目大意:有一个排队,右边的队头,左边的是队尾。给出每个排队者的年龄,年龄大的并不满意有更年轻的排在前面,所以很不满意。一个海象的不满意度是 排它前面比它年轻的所有海象中离它的最远的那个与它的距离-1。求出所有海狮的不满意度,如果前面没有更年轻的输出-1。

分析:我们设一只海象前面最远的年轻海象位置为right。我们考虑DP的思想,如果年龄为 i 的海象的right求出,那么年龄为 i+1的海象优先考虑以 i 的right作为它自己的right,因为如果 i 的right 都在i+1的左边,则剩下的更不可能在其右边。如果不满足 i 的right在i+1右边,那么只能说明i+1右边没有更年轻的,否则这个更年轻的会
i 的right,所以right设置为 i+1 它自己的位置。

一旦一个海狮的位置和最右都确定了,则直接算出来即可。注意要按照age排序而不是pos。

上代码:

#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;
}

其实也有单调队列的方法,我觉得比较难理解,大概思路。队列从排队最前端,依次只插入比队尾小元素,以保持单调递减(因为如果比前面的数大,那么最远的那个肯定不会是当前这个,所以当前这个插入了队列也没用)。对于一个大于队列尾的海象,用lower_bound去找比它小的且最靠近的海象,然后用位置一减即可。

上代码:

#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;
}

啦啦啦啦啦啦啊,继续加油!

时间: 2024-12-28 00:49:32

解题报告 之 CodeForces 91B Queue的相关文章

解题报告 之 CodeForces 6E Exposition

解题报告 之 CodeForces 6E Exposition Description There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction

CodeForces 91B Queue (线段树单点操作)

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

【解题报告】Codeforces Round #350 (Div. 2)

题目链接 A.Holidays(Codeforces 670A) 思路 首先,若 7 能整除 n 的话,最小假期天数 m1 与最大假期天数 m2 都等于 2×n7 ."整除"提示我们可以根据 n 除以 7 的余数来分类: 余数为 0 , m1=m2=2×n7 . 余数为 1 ,考虑多出来的那天是不是周末, m1=2×n7,m2=2×n7+1 . 余数为 6 ,考虑多出来的6天中必然有周末,考虑有1天周末还是2天周末, m1=2×n7+1,m2=2×n7+2 . 其它余数,考虑多出来的那

【解题报告】Codeforces Round #301 (Div. 2) 之ABCD

A. Combination Lock 拨密码..最少次数..密码最多有1000位. 用字符串存起来,然后每位大的减小的和小的+10减大的,再取较小值加起来就可以了... #include<stdio.h> #include<math.h> #include<string.h> #include<iostream> #include<algorithm> #include<map> #include<set> #inclu

Codeforces Round #615(Div.3)解题报告

Codeforces Round #615(Div.3)解题报告 A. Collecting Coins 注意\(n\)可能不够用的情况. #include<bits/stdc++.h> using namespace std; typedef long long ll; int a, b, c, n; void solve() { cin >> a >> b >> c >> n; int mx = max(max(a, b), c); int

codeforces 505A. Mr. Kitayuta&#39;s Gift 解题报告

题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母,使得新得到的字符串成为回文串. /**************************************(又到自我反省时刻) 做的时候,通过添加一个单位使得长度增加1,找出中点,检验前一半的位置,找出对称位置替换成对应的前一半位置的字符,然后原字符串剩下的部分追加到后面,再判断回文.但是由于

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

codeforces 591A. Wizards&#39; Duel 解题报告

题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be-Named 分别在走廊末端,各发射自己的impulse,其中Harry 的 impulse 速度为 p 米/s,He-...-Named (这个名字太长,姑且写成这样)为 q米/s.然后相遇之后各自回到它们的主人身边,再发射,问第二次相遇的时候,Harry的impulse 离他的位置距离是多少.

Codeforces 459(#261 (Div. 2) ) 解题报告

A:给你一个正方形的两点,让你求其余亮点 解法:乱搞 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月15日 星期五 23时24分04秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<