Codeforces Round #549 (Div. 2)

传送门直接点击名字即可

A – The Doors

题目大意:0和1分别代表左门和右门,当所有的左门或者右门都打开则视为到达目的,输出最少要打开几扇门

题解:应该算简单的模拟题

误:

错因分析:我这么写可能有一种情况有一扇左门或者右门在十分后面,该题左门和右门的数量是没有限制和要求的

例如:

8

1 1 1 1 1 0 0 1

正确的输出应该是7而该程序的输出是4

Input

5
1 1 0 0 0

Output

3

Answer

2

Checker Log

wrong answer 1st numbers differ - expected: '2', found: '3'
这种也是过不去的
#include <iostream>
using namespace std;
int main()
{
    long long t, x, a[2] = { 0 , 0 }, f = 0;
    cin >> t;
    for(int i = 1; i <= t; i++)
    {
        cin >> x;
        a[x] ++;
        if( a[x] >= t/2 && !f)
        {
            f = i;
        }

    }
    if(t==1||t==0){
            cout<<t<<endl;
        return 0;
    }
    if( t%2 == 1)
        cout << f+1 << endl;
    else
        cout << f << endl;

}

正确:

题解:找到0或1的最后一个位置比较谁的位置靠前则那即为最小的开门数

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int n;
    cin >> n;
    int ans1 = -1, ans2 = -1;
    int x;
    for (int i=1; i <= n; i++)
    {
        cin >> x;
        if(x){
             ans1 = i;
        }
        else{
            ans2 = i;
        }
    }
    cout << min(ans2, ans1) << endl;
return 0;
}
原文地址:https://www.cnblogs.com/ygbrsf/p/12583016.html