题解: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 n n and k k k chips. He realized that all these k k 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 i i-th row and j j j-th column as ( i , j ) (i ,j) (i,j). A diagonal is the set of cells for which the value i + j i + j i+j is the same. For example, cells ( 3 , 1 ) (3, 1) (3,1), ( 2 , 2 ) (2, 2) (2,2), and ( 1 , 3 ) (1, 3) (1,3) lie on the same diagonal, but ( 1 , 2 ) (1, 2) (1,2) and ( 2 , 3 ) (2, 3) (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 k k chips.

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

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

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

Input

Each test consists of several sets of input data. The first line contains a single integer t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500) — 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 n n, k k k ( 1 ≤ n ≤ 100 , 0 ≤ k ≤ n 2 1 \le n \le 100, 0 \le k \le n^2 1n100,0kn2) — the side of the checkered board and the number of available chips, respectively.

输入

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

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

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 k k chips.

输出

对于每组输入数据,输出一个整数 - 在放置所有 k k 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 ) (2, 1), (1, 2) (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 ) (1, 2), (2, 1), (1, 1) (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 ) (2, 1), (1, 2) (2,1),(1,2) 上,所以答案是 1。在第三个测试案例中,3 个筹码不能放置在一条对角线上,但是将它们放置在 ( 1 , 2 ) , ( 2 , 1 ) , ( 1 , 1 ) (1, 2), (2, 1), (1, 1) (1,2),(2,1),(1,1) 上会占据 2 条对角线。在第 7 个测试情形中,无论如何放置,筹码都会占据所有 5 条对角线。

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

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

  1. 1 1 1 条长度为 n n n 的对角线
  2. 长度从 1 1 1 n − 1 n-1 n1 的对角线分别都有 2 2 2

那我们只需要从 n n 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;
}

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18322470
博主已同意,我就是博主

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值