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:

cpp 复制代码
#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;
}
相关推荐
cike_y3 分钟前
Spring5入门&IOC容器
java·开发语言·spring·jdk·ioc·jdk1.8
你疯了抱抱我5 分钟前
【QQ】空间说说批量删除脚本(不用任何额外插件,打开F12控制台即可使用)
开发语言·前端·javascript
Tandy12356_9 分钟前
手写TCP/IP协议栈——实现ping响应不可达
c语言·网络·c++·网络协议·tcp/ip·计算机网络
沐知全栈开发23 分钟前
Web 词汇表
开发语言
程芯带你刷C语言简单算法题23 分钟前
Day37~求组合数
c语言·开发语言·学习·算法·c
程序员-周李斌26 分钟前
transmittable-thread-local[线程池跨线程值传递]
java·开发语言·算法·散列表
范纹杉想快点毕业27 分钟前
入门工程师指南:基于CRC校验的通信协议底层C语言实现
c语言·开发语言·mongodb
亓才孓32 分钟前
【homework1】彩票奖金问题(苛刻条件变松弛条件需要避免条件重复)
java·开发语言
wbs_scy36 分钟前
C++:unordered_map/unordered_set 使用指南(差异、性能与场景选择)
开发语言·c++·哈希算法
没有bug.的程序员40 分钟前
微服务网关:从“必选项”到“思考题”的深度剖析
java·开发语言·网络·jvm·微服务·云原生·架构