AtCoder题解——Beginner Contest 179——B - Go to Jail

题目相关

题目链接

AtCoder Beginner Contest 179 B 题,https://atcoder.jp/contests/abc179/tasks/abc179_b

Problem Statement

Tak performed the following action NN times: rolling two dice. The result of the ii-th roll is D_{i,1} and D_{i,2}.

Check if doublets occurred at least three times in a row. Specifically, check if there exists at lease one ii such that D{i,1}=D{i,2}D{i+1,1}=D{i+1,2} and D{i+2,1}=D{i+2,2} hold.

Input

Input is given from Standard Input in the following format:

N
D1,1 D1,2
⋮
DN,1 DN,2

Output

Print Yes if doublets occurred at least three times in a row. Print No otherwise.

Samples1

Sample Input 1

5
1 2
6 6
4 4
3 3
3 2

Sample Output 1

Yes

Explaination

From the second roll to the fourth roll, three doublets occurred in a row.

Samples2

Sample Input 2

5
1 1
2 2
3 4
5 5
6 6

Sample Output 2

No

Samples3

Sample Input 3

6
1 1
2 2
3 3
4 4
5 5
6 6

Sample Output 3

Yes

Constraints

  • 3 ≤ N≤100
  • 1 ≤ Di,j ≤ 6
  • All values in input are integers.

题解报告

题目翻译

给 N 个行,每行 2 个数,这样构成一个 N*2 的矩阵,问矩阵中是否存在连续三行,满足 Di1=Di2。

题目分析

非常简单的开胃题,直接遍历数组即可得到答案。

样例数据分析

样例 1

第 2、3、4 行的 Di,1 = Di,2,因此输出 Yes。

样例 2

没有连续的三行满足条件,因此输出 Yes。

样例 3

第 1、2、3 行的 Di,1 = Di,2,因此输出 Yes。

AC 参考代码

//https://atcoder.jp/contests/abc179/tasks/abc179_b
#include <bits/stdc++.h>
 
using namespace std;
 
const int MAXN=1e2+4;
int sz[MAXN][2+2];
 
int main() {
    int n;
    cin>>n;
    for (int i=1; i<=n; i++) {
        cin>>sz[i][1]>>sz[i][2];
    }

    for (int i=1; i<=n-2; i++) {
        if (sz[i][1]==sz[i][2] && sz[i+1][1]==sz[i+1][2] && sz[i+2][1]==sz[i+2][2]) {
            cout<<"Yes\n";
            return 0;
            }         
    }
    cout<<"No\n";
 
    return 0;
}

时间复杂度

O(N)。

猜你喜欢

转载自blog.csdn.net/justidle/article/details/108689154