Codeforces Round #549 (Div. 2)A. The Doors
1 second
256 megabytes
standard input
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.
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.
Print the smallest integer kk such that after Mr. Black opened the first kk doors, he was able to exit the house.
5
0 0 1 0 0
3
4
1 0 0 1
3
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的更多相关文章
- [题解] Codeforces Round #549 (Div. 2) B. Nirvana
Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...
- Codeforces Round #549 (Div. 1)
今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The B ...
- Codeforces Round #549 (Div. 2) 训练实录 (5/6)
The Doors +0 找出输入的01数列里,0或者1先出完的的下标. Nirvana +3 输入n,求1到n的数字,哪个数逐位相乘的积最大,输出最大积. 思路是按位比较,从低到高,依次把小位换成全 ...
- CodeForces Round #549 Div.2
A. The Doors 代码: #include <bits/stdc++.h> using namespace std; ; int N; , One = ; int a[maxn], ...
- [ 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 ...
- Codeforces Round #549 (Div. 2) Solution
传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ...
- Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)
https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...
- Codeforces Round #549 (Div. 2) E 倍增处理按排列顺序的上一个位置
https://codeforces.com/contest/1143/problem/E 题意 p为n的一个排列,给出有m个数字的数组a,q次询问,每次询问a数组区间[l,r]中是否存在子序列为p的 ...
- 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为起始位 ...
随机推荐
- 用css实现图片在div内的垂直居中
已知一个div内有一个img,两者的高度均不知道,但图片的高度一定小于div的高度 代码如下: .box { /*非IE的主流浏览器识别的垂直居中的方法*/ display: table-cell; ...
- 用python40行代码编写的计算器
效果图 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
- redis学习六 集群的原理(转载)
转载自 http://shift-alt-ctrl.iteye.com/blog/2285470 一.Redis Cluster主要特性和设计 集群目标 1)高性能和线性扩展,最大可以支撑到1 ...
- C++深度解析教程学习笔记(6)对象的构造和销毁
1. 对象的初始化 (1)从程序设计的角度看,对象只是变量,因此: ①在栈上创建对象时,成员变量初始化为随机值 ②在堆上创建对象时,成员变量初始化为随机值 ③在静态存储区创建对象时,成员变量初始化为 ...
- python3 破解 geetest(极验)的滑块验证码
Kernel_wu 快速学习的实践者 python3 破解 geetest(极验)的滑块验证码 from selenium import webdriver from selenium.webdriv ...
- Unity shader saturate
当你想将颜色值规范到0~1之间时,你可能会想到使用saturate函数(saturate(x)的作用是如果x取值小于0,则返回值为0.如果x取值大于1,则返回值为1.若x在0到1之间,则直接返回x的值 ...
- Android Studio 编译提示 No installed build tools found. Please install the Android build tools
添加 ANDROID_HOME=D:\Android\adt-bundle-windows\sdk 系统变量即可
- POI 生成exel报表
去官网下载相关jar包 http://poi.apache.org/ package poi.zr.com; import java.io.File; import java.io.FileNot ...
- opennebula 创建镜像数据块
{","csrftoken":"f5454a02dea7b4a7d5d50b482a762b57"}
- Hadoop完全分布式环境搭建(三)——基于Ubuntu16.04安装和配置Java环境
[系统环境] 1.宿主机OS:Win10 64位 2.虚拟机软件:VMware WorkStation 12 3.虚拟机OS:Ubuntu16.04 4.三台虚拟机 5.JDK文件:jdk-8u201 ...