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

 
5
0 0 1 0 0
Output

 
3
Input

 
4
1 0 0 1
Output
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.

解题思路:这道题就是给你n个数,0代表左边的门,1代表右边的门,只有把左边或者右边全部的门全部打开才可逃离,问最快逃离的序号;

实际上我们只需遍历一遍,看是先把左边的门遍历完还是右边的门遍历完即可,再输出答案;

 #include<iostream>
#include<string.h>
using namespace std; int n ;
int a[];
int olen = ; //0的个数,左边门的个数;
int ylen = ; //1的个数,右边门的个数;
int tmpo = ; //用来遍历统计左边的门的个数;
int tmpy = ; //用来遍历统计左边的门的个数;
int main()
{
cin>>n;
for(int i = ; i <= n ; i++ )
{
cin>>a[i];
if(a[i]==)
{
ylen++;
}else
olen++;
}
for(int i = ; i <= n ; i++)
{
if(a[i]==)
{
tmpy++;
}else
if(a[i]==)
tmpo++; if(tmpy==ylen) //用来遍历统计左边的门的个数;如果左边先遍历完则输出;
{
cout<<i;
break;
}else
if(tmpo==olen) //右边同理;
{
cout<<i;
break;
}
}
return ;
}

代码如下:

Codeforces Round #549 (Div. 2)A. The Doors的更多相关文章

  1. [题解] Codeforces Round #549 (Div. 2) B. Nirvana

    Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...

  2. Codeforces Round #549 (Div. 1)

    今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The B ...

  3. Codeforces Round #549 (Div. 2) 训练实录 (5/6)

    The Doors +0 找出输入的01数列里,0或者1先出完的的下标. Nirvana +3 输入n,求1到n的数字,哪个数逐位相乘的积最大,输出最大积. 思路是按位比较,从低到高,依次把小位换成全 ...

  4. CodeForces Round #549 Div.2

    A. The Doors 代码: #include <bits/stdc++.h> using namespace std; ; int N; , One = ; int a[maxn], ...

  5. [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]

    https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...

  6. Codeforces Round #549 (Div. 2) Solution

    传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ...

  7. Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

    https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...

  8. Codeforces Round #549 (Div. 2) E 倍增处理按排列顺序的上一个位置

    https://codeforces.com/contest/1143/problem/E 题意 p为n的一个排列,给出有m个数字的数组a,q次询问,每次询问a数组区间[l,r]中是否存在子序列为p的 ...

  9. Codeforces Round #549 (Div. 2) D 数学

    https://codeforces.com/contest/1143/problem/D 题意 有nk个城市,第1,k+1,2k+1,...,(n-1)k+1城市有餐厅,你每次能走l距离,a为起始位 ...

随机推荐

  1. ORACLE和MYSQL函数

    函数 编号 类别 ORACLE MYSQL 注释 1 数字函数 round(1.23456,4) round(1.23456,4) 一样: ORACLE:select round(1.23456,4) ...

  2. C#通用模块专题

    开源 程序设计 常用组件 加载图片,播放音乐.视频,摄像头拍照 文件读写(txt.xml.自定义文件格式(后缀名)) 串口通信 稳定的串口读写:http://blog.csdn.net/kolvin2 ...

  3. Java微信公众平台开发【番外篇】(七)--公众平台测试帐号的申请

    转自:http://www.cuiyongzhi.com/post/45.html 前面几篇一直都在写一些比较基础接口的使用,在这个过程中一直使用的都是我个人微博认证的一个个人账号,原本准备这篇是写[ ...

  4. django表单的字段验证(clean_<fieldname>())和ajax的字段验证

    django中的Form有个很重要的功能:验证用户输入 而验证用户输入也可以分为2种: (1)前端本身的验证,例如:字段是否可为空,手机号码格式是否正确等: (2)前端输入数据和后台数据库数据的验证, ...

  5. 【原创】ubuntu14.04 LTS系统VMware虚拟机内Windows server 2008 r2系统的网络配置

    主系统:ubuntu14.04 LTS 虚拟机:VMware 内系统:Windows server 2008 r2 主系统IP:任何IP 内系统IP:192.168.202.128(自动获取,如果不是 ...

  6. Hadoop IO 特性详解(1)

    本文结合hadoop : the definitive guide精心而作,包含作者的心血,希望可以帮助大家理解一点hdfs的皮毛,足矣.(charles@xingbod.cn) hadoop本身自带 ...

  7. eclipse生成的web项目在resin服务器上的发布(不能解析web.xml)

    首先报错误: web项目web.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <web-ap ...

  8. BootstrapValidator:表单验证神器

    前言:做Web开发的我们,表单验证是再常见不过的需求了.友好的错误提示能增加用户体验.博主搜索bootstrap表单验证,搜到的结果大部分都是文中的主题:bootstrapvalidator.今天就来 ...

  9. 使用mail架包发送邮件javax.mail.AuthenticationFailedException: failed to connect at javax.mail.Service.connec

    这个错误是因为连接不上邮箱服务器导致的,可能有以下几个原因(以网易邮箱为例) 1.当使用第三方登录邮箱时需要有邮箱的授权码,且要开启POP3/SMTP/IMAP:服务 2.在代码中要调用网易邮箱的密码 ...

  10. 22.NULL 值

    NULL 值是遗漏的未知数据. 默认地,表的列可以存放 NULL 值. 本章讲解 IS NULL 和 IS NOT NULL 操作符. SQL NULL 值 如果表中的某个列是可选的,那么我们可以在不 ...