jiejiejiang2004

题解:Codeforces Round 961 (Div. 2) A

A. Diagonals

*time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Vitaly503 is given a checkered board with a side of \(n\) and \(k\) chips. He realized that all these \(k\) chips need to be placed on the cells of the board (no more than one chip can be placed on a single cell).

Let's denote the cell in the \(i\)-th row and \(j\)-th column as \((i ,j)\). A diagonal is the set of cells for which the value \(i + j\) is the same. For example, cells \((3, 1)\), \((2, 2)\), and \((1, 3)\) lie on the same diagonal, but \((1, 2)\) and \((2, 3)\) do not. A diagonal is called occupied if it contains at least one chip.

Determine what is the minimum possible number of occupied diagonals among all placements of \(k\) chips.

Vitaly503 获得了一个边长为 \(n\) 的棋盘和 \(k\) 个棋子。他意识到所有这些 \(k\) 个棋子都需要被放置在棋盘的格子上(每个格子最多只能放置一个棋子)。

我们定义第 \(i\) 行第 \(j\) 列的格子为 \((i, j)\)。对角线是指那些满足 \(i + j\) 值相同的格子集合。例如,格子 \((3, 1)\)\((2, 2)\)\((1, 3)\) 位于同一条对角线上,但 \((1, 2)\)\((2, 3)\) 不在。如果一条对角线上至少有一个棋子,那么这条对角线就被称为被占据的。

请确定在所有 \(k\) 个棋子的放置方式中,被占据的对角线的最小可能数量是多少。

Input

Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of sets of input data. Then follow the descriptions of the sets of input data.

The only line of each set of input data contains two integers \(n\), \(k\) (\(1 \le n \le 100, 0 \le k \le n^2\)) — the side of the checkered board and the number of available chips, respectively.

输入

每个测试由多组输入数据组成。第一行包含一个整数 \(t\) ( \(1 \le t \le 500\) )--输入数据集的数量。然后是各组输入数据的说明。

每组输入数据的唯一一行包含两个整数 \(n\)\(k\) ( \(1 \le n \le 100, 0 \le k \le n^2\) ) - 分别是棋盘的边数和可用筹码数。

Output

For each set of input data, output a single integer — the minimum number of occupied diagonals with at least one chip that he can get after placing all \(k\) chips.

输出

对于每组输入数据,输出一个整数 - 在放置所有 \(k\) 个筹码后,他能得到的至少有一个筹码的对角线的最小占位数。

Example
input

7
1 0
2 2
2 3
2 4
10 50
100 239
3 9

output

0
1
2
3
6
3
5

Note

In the first test case, there are no chips, so 0 diagonals will be occupied. In the second test case, both chips can be placed on diagonal \((2, 1), (1, 2)\), so the answer is 1. In the third test case, 3 chips can't be placed on one diagonal, but placing them on \((1, 2), (2, 1), (1, 1)\) makes 2 diagonals occupied. In the 7th test case, chips will occupy all 5 diagonals in any valid placing.

在第一个测试案例中,没有筹码,因此 0 个对角线将被占据。在第二个测试案例中,两个筹码都可以放置在对角线 \((2, 1), (1, 2)\) 上,所以答案是 1。在第三个测试案例中,3 个筹码不能放置在一条对角线上,但是将它们放置在 \((1, 2), (2, 1), (1, 1)\) 上会占据 2 条对角线。在第 7 个测试情形中,无论如何放置,筹码都会占据所有 5 条对角线。

题解
这题就是一道很明显的贪心
为了让占据的对角线数量尽量小
我们应该首先占据满更长的对角线

我们在看一下对角线分布的规律
一个边长为 \(n\) 的矩形包含有

  1. \(1\) 条长度为 \(n\) 的对角线
  2. 长度从 \(1\)\(n-1\) 的对角线分别都有 \(2\)

那我们只需要从 \(n\) 开始向外扩散就好了(提示:用while循环一下暴力就可以了)

我的代码

#include <bits/stdc++.h>

const int N1 = 1e4 + 10;
int t,n,k;
int bii[N1];

void solve() {
    std::cin >> n >> k;
    int cnt = 0;
    int tem = n;

    if(k) {
        k -= tem;
        cnt ++;
        tem--;
    }
    
    while(k > 0 && tem) {
        if(k >= tem * 2) {
            k -= tem * 2;
            cnt += 2;
        } else {
            k -= tem;
            cnt ++;
        }
        tem --;
    }

    std::cout << cnt << "\n";
}

signed main() {
    std::cin >> t;
    while(t--) solve();
    return 0;
}

posted on 2024-07-25 10:33  Jiejiejiang  阅读(16)  评论(0编辑  收藏  举报

导航