/// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
/// __.' ~. .~ `.__
/// .'// \./ \\`.
/// .'// | \\`.
/// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
/// .'//.-" `-. | .-' "-.\\`.
/// .'//______.============-.. \ | / ..-============.______\\`.
/// .'______________________________\|/______________________________`.
There are (n+1) rooms in the dungeon, consequently connected by n passages. The rooms are numbered from 0 to n, and the passages from 1 to n. The i-th passage connects rooms (i−1) and i.
Every passage is equipped with a security device of one of two types. Security device of the first type checks if a person who walks through the passage has the pass of the certain color, and forbids movement if such pass is invalid. Security device of the second type never forbids movement, but the pass of the certain color becomes invalid after the person walks through.
In the beginning you are located in the room s and have all the passes. For every s from 0 to (n−1) find how many rooms you can walk through in the direction of the room n.
Input
The first line contains an integer n (1≤n≤500000) — the number of passages.
The second line contains n integers ai (1≤|ai|≤n). The number |ai| is a color of the pass which the i-th passage works with. If ai>0, the i-th passage is the first type passage, and if ai<0 — the second type.
Output
Output n integers: answers for every s from 0 to (n−1).
Examples
Input
6
1 -1 -1 1 -1 1
Output
3 2 1 2 1 1
Input
7
2 -1 -2 -3 1 3 2
Output
4 3 3 2 3 2 1
反向遍历
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<stack>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=550000;
int n;
int pos[N];
int rooms[N];
int a[N];
int main()
{
while(~scanf("%d",&n))
{
memset(rooms,0,sizeof(rooms));
memset(pos,0,sizeof(pos));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int temp=inf;
for(int i=n-1;i>=0;i--)
{
if(a[i]>0)
{
rooms[i]=rooms[i+1]+1;
pos[a[i]]=i;//记录a[i]出现的最靠前的位置
}
else
{
if(!pos[-a[i]]) rooms[i]=rooms[i+1]+1; //如果在i后边没有这种颜色,可通过就是下一个情况加1。
else
{
temp=min(temp,pos[-a[i]]);// 寻找能到达的最靠前的位置
rooms[i]=temp-i;
}
}
}
for(int i=0;i<n;i++)
{
printf("%d%c",rooms[i],i==n-1?'\n':' ');
}
}
return 0;
}