atcoder ABC 355-C题详解

atcoder ABC 355-C题详解

Problem Statement

There is an N×N grid, where the cell at the i-th row from the top and the j-th column from the left contains the integer N×(i−1)+j.

Over T turns, integers will be announced. On Turn i, the integer Ai​ is announced, and the cell containing Ai​ is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T turns, print -1.

Here, achieving Bingo means satisfying at least one of the following conditions:

There exists a row in which all N cells are marked.
There exists a column in which all N cells are marked.
There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N cells are marked.

Constraints

2≤N≤2×103
1≤T≤min(N2,2×105)
1≤Ai​≤N2
Ai​=Aj​ if i=j.
All input values are integers.

Input

The input is given from Standard Input in the following format:

Output

If Bingo is achieved within T turns, print the turn number on which Bingo is achieved for the first time; otherwise, print -1.

Sample Input 1

3 5
5 1 8 9 7

Sample Output 1

4

The state of the grid changes as follows. Bingo is achieved for the first time on Turn 4.

在这里插入图片描述

Sample Input 2

​3 5
4 2 9 7 5

Sample Output 2

-1
Bingo is not achieved within five turns, so print -1.

Sample Input 3

4 12
13 9 6 5 2 7 16 14 8 3 10 11

Sample Output 3

9

思路分析:

先判断输入的数字在哪一行那一列,然后判断是否符合宾果游戏规则,判断主对角线和次对角线以及行、列。

code:

#include <iostream>
using namespace std;
const int N = 2010;
int n, t;
int row[N];
int col[N];
int ans1, ans2;
int main() {
    cin >> n >> t;
    for (int i = 0; i < t; i++) {
        int a;
        cin >> a;
        a--;//要减一比如5,5/3=1,5%3=2
        int row1 = a / n;
        int col1 = a % n;
//        if (row1 >= n || col1 >= n) {不需要比如第一个样例9/3=3 直接输出-1结束程序了
//            cout << "-1" << endl;
//            return 0;
//        }
        row[row1]++;
        col[col1]++;
        // 检查主对角线
        if (row1 == col1) {
         ans1++;
        }
        // 检查次对角线
        if (row1 + col1 == n - 1) {
         ans2++;
        }
        if(ans1 == n || ans2 == n) {
         cout << i + 1 << endl;
         return 0;
  }
        // 检查行和列
        if (row[row1] == n || col[col1] == n) {
            cout << i + 1 << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
    return 0;
}

相关推荐

  1. atcoder ABC 358-B详解

    2024-06-15 19:12:01       33 阅读
  2. LeetCode(66,69,35,88)--《c++》

    2024-06-15 19:12:01       26 阅读
  3. Redis面试35

    2024-06-15 19:12:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 19:12:01       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 19:12:01       97 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 19:12:01       78 阅读
  4. Python语言-面向对象

    2024-06-15 19:12:01       88 阅读

热门阅读

  1. 运维监控系统

    2024-06-15 19:12:01       33 阅读
  2. ArrayList<Integer>()转为int[]的几种方式

    2024-06-15 19:12:01       43 阅读
  3. c++_0基础_讲解5 判断语句

    2024-06-15 19:12:01       34 阅读
  4. 算法训练营day23补签

    2024-06-15 19:12:01       33 阅读
  5. ISO七层模型 tcp/ip

    2024-06-15 19:12:01       28 阅读
  6. 2022C语言二级真题

    2024-06-15 19:12:01       22 阅读
  7. TCP协议参数设置&说明

    2024-06-15 19:12:01       27 阅读
  8. 探索机器学习:深入理解Sklearn基础

    2024-06-15 19:12:01       28 阅读
  9. vue3delete请求报403forbidden,前后端解决方式

    2024-06-15 19:12:01       24 阅读