A. The Doors

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.

There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index kk such that Mr. Black can exit the house after opening the first kk doors.

We have to note that Mr. Black opened each door at most once, and in the end all doors became open.

Input

The first line contains integer nn (2≤n≤2000002≤n≤200000) — the number of doors.

The next line contains nn integers: the sequence in which Mr. Black opened the doors. The ii-th of these integers is equal to 00 in case the ii-th opened door is located in the left exit, and it is equal to 11 in case it is in the right exit.

It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.

Output

Print the smallest integer kk such that after Mr. Black opened the first kk doors, he was able to exit the house.

Examples

input

Copy

5
0 0 1 0 0

output

Copy

3

input

Copy

4
1 0 0 1

output

Copy

3

Note

In the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.

When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.

In the second example when the first two doors were opened, there was open closed door in each of the exit.

With three doors opened Mr. Black was able to use the left exit.

 

解题说明:此题其实是找出队列中最迟出现的0或1。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;


int main()
{
	int n, i, a, l = 0, r = 0;
	scanf("%d", &n);
	for (i = 0; i<n; ++i)
	{
		scanf("%d", &a);
		if (a == 0)
		{
			l = i + 1;
		}
		else
		{
			r = i + 1;
		}
	}
	if (l < r)
	{
		printf("%d\n", l);
	}
	else
	{
		printf("%d\n", r);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值