• Codeforces Round #549 (Div. 2)A. The Doors


    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 (2n2000002≤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代表右边的门,只有把左边或者右边全部的门全部打开才可逃离,问最快逃离的序号;

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

     1 #include<iostream>
     2 #include<string.h>
     3 using namespace std;
     4 
     5 int n ;
     6 int a[200005];
     7 int olen = 0;         //0的个数,左边门的个数;
     8 int ylen = 0;         //1的个数,右边门的个数;
     9 int tmpo = 0;       //用来遍历统计左边的门的个数;
    10 int tmpy = 0;      //用来遍历统计左边的门的个数;
    11 int main()
    12 {
    13     cin>>n;
    14     for(int i = 1 ; i <= n ; i++ )
    15     {
    16         cin>>a[i];
    17         if(a[i]==1)
    18         {
    19             ylen++;
    20         }else
    21         olen++;
    22     }
    23     for(int i = 1 ; i <= n ; i++)
    24     {
    25         if(a[i]==1)
    26         {
    27             tmpy++;
    28         }else
    29         if(a[i]==0)
    30         tmpo++;
    31         
    32         if(tmpy==ylen)  //用来遍历统计左边的门的个数;如果左边先遍历完则输出;
    33         {
    34             cout<<i;
    35             break;
    36         }else
    37         if(tmpo==olen)   //右边同理;
    38         {
    39             cout<<i;
    40             break;
    41         }
    42     }
    43     return 0;
    44 }

    代码如下:

  • 相关阅读:
    ASP与sql存储过程
    SharpWebMail介绍和安装(转)
    安全编程: 验证输入
    【转】 数据库备份与还原处理
    权限管理设计、分析、实现参考资料
    权限设计
    提高查询速度方法总结
    乱七八糟知识点
    阿里分布式事务框架Seata原理解析
    分布式事务
  • 原文地址:https://www.cnblogs.com/yewanting/p/10727214.html
Copyright © 2020-2023  润新知