A. Rooms and Passages 【思维】

///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
///             __.'              ~.   .~              `.__
///           .'//                  \./                  \\`.
///        .'//                      |                     \\`.
///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
///     .'//.-"                 `-.  |  .-'                 "-.\\`.
///   .'//______.============-..   \ | /   ..-============.______\\`.
/// .'______________________________\|/______________________________`.



传送门


A. Rooms and Passages
time limit per test2.0 s
memory limit per test256 MB
inputstandard input
outputstandard output

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值