A15028 | Half Queen Cover
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a board with $n$ rows and $n$ columns, numbered from $1$ to $n$ . The intersection of the $a$ -th row and $b$ -th column is denoted by $(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)$ attacks the cell $(c, d)$ if $a=c$ or $b=d$ or $a-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.
A half-queen attacks cells in the same row, same column, and on one diagonal. More formally, a half-queen on $(a, b)$ attacks the cell $(c, d)$ if $a=c$ or $b=d$ or $a-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.
输入格式
The first line contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the size of the board.
输出格式
In the first line print a single integer $k$ — the minimum number of half-queens.
In each of the next $k$ lines print two integers $a_i$ , $b_i$ ( $1 \le a_i, b_i \le n$ ) — the position of the $i$ -th half-queen.
If there are multiple solutions, print any.
In each of the next $k$ lines print two integers $a_i$ , $b_i$ ( $1 \le a_i, b_i \le n$ ) — the position of the $i$ -th half-queen.
If there are multiple solutions, print any.
输入输出样例
输入 #1
1
输出 #1
1 1 1
输入 #2
2
输出 #2
1 1 1
输入 #3
3
输出 #3
2 1 1 1 2
Example $1$ : one half-queen is enough. Note: a half-queen on $(1, 1)$ attacks $(1, 1)$ .
Example $2$ : one half-queen is enough too. $(1, 2)$ or $(2, 1)$ would be wrong solutions, because a half-queen on $(1, 2)$ does not attack the cell $(2, 1)$ and vice versa. $(2, 2)$ is also a valid solution.
Example $3$ : it is impossible to cover the board with one half queen. There are multiple solutions for $2$ half-queens; you can print any of them.
Example $2$ : one half-queen is enough too. $(1, 2)$ or $(2, 1)$ would be wrong solutions, because a half-queen on $(1, 2)$ does not attack the cell $(2, 1)$ and vice versa. $(2, 2)$ is also a valid solution.
Example $3$ : it is impossible to cover the board with one half queen. There are multiple solutions for $2$ half-queens; you can print any of them.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted