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为起始位 ...
随机推荐
- HTTP 与TCP/IP 、Socket区别(一)
网络由下往上分为: 物理层-- 数据链路层-- 网络层-- IP协议 传输层-- TCP协议 会话层-- 表示层和应用层-- HTTP协议 1.TCP/IP连接 手机能够使用联网功能是因为手机底层实现 ...
- 解决word自动编号出现内容空格过大的问题
选择你需要调整的段落.右键点击.选择“调整列表缩进” 然后在弹出的窗口中,第三行的“制表符”改成“空格”即可.或者空格都不需要可以改为“不特别标注”. 当编号超过10的时候,也会有空格太大的现象,这时 ...
- leetcode427
本题不会做,从网上找到了python3的解法,记录如下. class Solution: def construct(self, grid): def dfs(x, y, l): if l == 1: ...
- PHP PDO SQLSERVER
$bbs = new PDO("odbc:MSSQLServer", $username_bbs, $password_bbs $bbs = new PDO('); $s ...
- Checker Challenge跳棋的挑战(n皇后问题)
Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...
- linux tcpdump补充
If they are going across the loopback interface, you may have to tell tcpdump to read that interface ...
- error:crosses initialization of ...的解决办法
switch(c) { case 0x01: int temp = a + b; .... break; case 0x02: break; default:break; } 此时会报如题所示错误 原 ...
- libevent源码分析
这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...
- instanceof php
instdnceof php5 的一个新成员 功能: 使用这个关键字可以确定一个对象是否是类的实例,是否是累的子类 ,还是实现了某个特定的接口. <?php class A{} class B ...
- IWebBrowser和IE浏览器的行为不一样
原本一直以为IWebBrowser2的行为和IE浏览器的行为应该是一样的,但是最近发现事实不是如此. IE8以后的浏览器都带有兼容模式,而IWebBrowser2默认情况下是在兼容模式下运行的,可以参 ...