CF1667C.Half Queen Cover

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a board with nn rows and nn columns, numbered from 11 to nn. The intersection of the aa-th row and bb-th column is denoted by (a,b)(a, b).

A half-queen attacks cells in the same row, same column, and on one diagonal. More formally, a half-queen on (a,b)(a, b) attacks the cell (c,d)(c, d) if a=ca=c or b=db=d or a−b=c−da-b=c-d.

The blue cells are under attack.

What is the minimum number of half-queens that can be placed on that board so as to ensure that each square is attacked by at least one half-queen?

Construct an optimal solution.

你有一个 nn 行 nn 列的棋盘,行列编号均为 11 到 nn。第 aa 行与第 bb 列的交点记为 (a,b)(a, b)。

一个“半后”(half-queen)可攻击其所在行、所在列以及其中一条对角线上的所有格子。更准确地说,位于 (a,b)(a, b) 的半后会攻击格子 (c,d)(c, d),当且仅当满足以下任一条件:a=ca=c 或 b=db=d 或 a−b=c−da-b=c-d。

图中蓝色格子处于攻击范围内。

问:在该棋盘上至少需要放置多少个半后,才能保证每个格子都至少被一个半后攻击到?

请构造一种最优方案。

输入格式

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the size of the board.

第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)——棋盘的大小。

输出格式

In the first line print a single integer kk — the minimum number of half-queens.

In each of the next kk lines print two integers aia_i, bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n) — the position of the ii-th half-queen.

If there are multiple solutions, print any.

第一行输出一个整数 kk —— 半皇后所需的最少数量。

接下来的 kk 行中,每行输出两个整数 aia_i、bib_i(1≤ai,bi≤n1 \le a_i, b_i \le n)—— 第 ii 个半皇后的位置。

若存在多种解,输出任意一种即可。

输入输出样例

  • 输入#1

    1

    输出#1

    1
    1 1
  • 输入#2

    2

    输出#2

    1
    1 1
  • 输入#3

    3

    输出#3

    2
    1 1
    1 2

说明/提示

Example 11: one half-queen is enough. Note: a half-queen on (1,1)(1, 1) attacks (1,1)(1, 1).

Example 22: one half-queen is enough too. (1,2)(1, 2) or (2,1)(2, 1) would be wrong solutions, because a half-queen on (1,2)(1, 2) does not attack the cell (2,1)(2, 1) and vice versa. (2,2)(2, 2) is also a valid solution.

Example 33: it is impossible to cover the board with one half queen. There are multiple solutions for 22 half-queens; you can print any of them.

示例 11:只需一个半皇后即可。注意:位于 (1,1)(1, 1) 的半皇后可以攻击 (1,1)(1, 1) 自身。

示例 22:同样只需一个半皇后即可。(1,2)(1, 2) 或 (2,1)(2, 1) 均为错误解,因为位于 (1,2)(1, 2) 的半皇后无法攻击格子 (2,1)(2, 1),反之亦然。(2,2)(2, 2) 也是一个合法解。

示例 33:无法仅用一个半皇后覆盖整个棋盘。使用 22 个半皇后则存在多种可行方案;你可以输出其中任意一种。

输入解题思路,AI测评打分。不知道怎么写?

首页