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为起始位 ...
随机推荐
- Sqoop导出MySQL数据
导入所有表: sqoop import-all-tables –connect jdbc:mysql://ip:port/dbName --username userName --password p ...
- 8.solr学习速成之FacetPivot
什么是Facet.pivot Facet.pivot就是按照多个维度进行分组查询,是Facet的加强,在实际运用中经常用到,一个典型的例子就是商品目录树 NamedList解释: NamedList ...
- new Com
CreateOleObject System.Win.ComObj.hpp #include <objbase.h> Winapi.ActiveX.pas CoInitiali ...
- webpack4+vue2+axios+vue-router的多页+单页混合应用框架
VUE2的单页应用框架有人分享了,多页应用框架也有人分享了,这里就分享一个单页和多页的混合应用框架吧,初现雏形,还有很多需要优化和改善的地方... 结尾有github地址. 项目结构 │ ├─buil ...
- Mac os JAVA 开发环境配置简述
本文原文来源:http://blog.csdn.NET/johnstrive/article/details/7791451 1.Mac 自带jdk1.6(本人的机器是这样至于新Mac自带的版本就不清 ...
- 一个由有符号下标引起的bug
先看段代码: if(s[d[i]]) { ... } 这里的d是一个char*的内存buffer,s是一个256长度的bool数组.上段代码逻辑是,s已进行过初始化,其作用是过滤字节,有些字节对应tr ...
- 【HDU4307】Matrix
本篇博客基本全篇转自https://www.cnblogs.com/staginner/archive/2012/08/13/2636826.html,太强啦ORZ 题意 A是个1*n的矩阵,每个元素 ...
- volatile和 锁的区别
Volatile: 当把变量声明为volatile类型后,编译器和运行时都会注意到这个变量是共享的,因此不会将该变量上的操作与其它内存操作一起重排序.volatile变量不会被缓存在寄存器或者对其他处 ...
- Zend Studio 10汉化方法
选择Help菜单->Install New Software...在Work with框中复制此地址:http://download.eclipse.org/technology/babel/u ...
- 线程dump
当应用程序运行变慢或者发生故障时,可能通过分析java的Thread Dumps得到分析他们得到阻塞和存在瓶颈的线程. 线程堆栈是虚拟机中线程(包括锁)状态的一个瞬间状态的快照,即系统在某一个时刻所有 ...