首页 > 其他分享 >题解:Codeforces Round 961 (Div. 2) A

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

时间:2024-07-25 10:42:46浏览次数:6  
标签:le 961 题解 对角线 test input Div chips tem

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;
}

标签:le,961,题解,对角线,test,input,Div,chips,tem
From: https://www.cnblogs.com/jiejiejiang2004/p/18322470

相关文章

  • Codeforces Round 961 (Div. 2)
    ABC没什么,除了B2还没补。主要就是这个D题。这题我基本上需要想到的都想到了,没想到的部分就是记录不合法答案而非直接记录正确答案。其实这思路也有点能够被启发的地方,就是只有某个位置前k个位置是又至少一个数字存在与我们的选择集合里的,我们才能够统计答案。也就是说,如果统计......
  • [题解]CF958C3 Encryption (hard)
    思路先考虑\(\Theta(n^2k)\)的暴力DP。定义\(dp_{i,j}\)表示在前\(i\)个数中选取\(j\)个的最小和,转移显然:\[dp_{i,j}=\min_{1\leqk<i}\{dp_{k,j-1}+s_{k+1,i}\bmodp\}\]注意到一个性质:\(dp_{i,j}\equivs_i\pmodp\)。因为前者是前\(i\)项分为若干......
  • Codeforces Round 961 (Div. 2)
    A.Diagonals----------------------------题解----------------------------------注意读题,题目中只有i+j相同的格子才是一个对角线,也就是说,(1,1)(2,2)(3,3)的那条大斜线不是个对角线,如图所示这是一个3*3的图中所有的对角线,那么我们只需要如图所示,从中间往两边依次放就可以,......
  • Java二叉树经典进阶OJ题解
     目录一、判断一颗二叉树是否为对称二叉树1.题目描述:2.代码示例:3.通过演示与分析:二、根据先序遍历结果构造二叉树1.题目描述:2.代码示例:3.通过演示与分析:三、层序遍历的非递归实现1.题目描述:2.代码示例:3.通过演示与分析:四、判断是否为完全二叉树1.题目描述:2.......
  • dify-on-wechat 数据乱串问题解决记录
    在检查dify-on-wechat应用中的数据乱串问题时,我们发现了一个关键因素:当前端和后端使用的大语言模型不一致时,会导致数据串扰问题。在深入调查和测试后,我们采取了将前后端的大语言模型改为一致的策略。在实施这一改变后,数据乱串的问题得到了有效解决。以下是详细的记录:问题现象:在dif......
  • 密码学-RSA基础题解题脚本-Python
    importgmpy2#计算大整数模块importlibnumimportrsafromCrypto.PublicKeyimportRSA#安装时安装pycryptodome模块#已知:p,q,e,cdefknown_p_q_e_c():p=int(input('请输入一个素数p:'))q=int(input('请输入另一个素数q:'))e=int(input('请输入公钥e:'))......
  • [题解]P9755 [CSP-S 2023] 种树
    P9755[CSP-S2023]种树迟来的补题本题是让最小化所有树长到指定高度日期的最大值,于是想到二分答案。那么,对于一个给定的期限\(x\),如何判断是否能在这个日期内完成任务呢?首先我们发现前\(n\)天每天都要种树,那么假设我们已经知道了每个地块最晚哪个日期种树,能保证在期限\(x\)......
  • codeforces div_2 961 题解报告
    codeforcesdiv_2961题解报告比赛链接:https://codeforces.com/contest/1995A.Diagonals题目翻译给定一个边长为\(n\)的正方形,给定\(k\),要往正方形选\(k\)个点,\(x+y\)相同的点构成对角线,问至少要几条对角线才能装下\(k\)个点。时限1s,空间限制256MB\(1\len\le100,0\l......
  • Codeforces Round 961 (Div. 2)
    A.JoeyTakesMoney--------------------------题解------------------------------------选取x和y替换掉a[i],a[j],前提是两者乘积相同,最后要求和数组最大,其实很简单,我们只需要不对另x=1y=a[j]*a[x]就行,从左到右遍历整个数组队a[i]和a[i+1]进行此操作,就可以得到我们想要的值......
  • 题解:牛客多校第三场 A
    ABridgingtheGap2时间限制:C/C++1秒,其他语言2秒空间限制:C/C++1048576K,其他语言2097152KSpecialJudge,64bitIOFormat:%lld题目描述Agroupof\(n\)walkersarrivesatariverbankatnight.Theywanttocrosstheriverusingaboat,whichisinitiallyont......