Codeforces Round 926 (Div. 2) B. Sasha and the Drawing (Java)

Codeforces Round 926 (Div. 2) B. Sasha and the Drawing (Java)

比赛链接:Codeforces Round 926 (Div. 2)

B题传送门:B. Sasha and the Drawing

题目:B. Sasha and the Drawing

在这里插入图片描述
Example
input

7
3 4
3 3
3 10
3 9
4 7
7 11
2 3

output

2
2
6
5
4
6
2

分析:

由题可知,要我们计算着色最小单元格数量。

经过分析,我们可知:

在 1到 4(n-1) 个对角线,着色1个单元格,最多可以获得2个对角线。

在 4(n-1)+1 到 4n-2 个对角线,着色1个单元格,最多可以获得1个对角线。

例如:
在这里插入图片描述
在 n=4 时,有6个单元格着色可以获得2个对角线,有2个单元格,着色可以获得1个对角线。
在这里插入图片描述

代码:

import java.util.*;

public class Main{
    public static void main(String[] args) {  
    	Scanner sc = new Scanner(System.in);
    	int tr = sc.nextInt();
    	while(tr-->0) {
    		int n = sc.nextInt();
    		int k = sc.nextInt();
    		// 计算获得2对彩色对角线的方块个数
    		int cnt2 = 2*(n-1);
    		int ans = 0;
    		if(k<=2*cnt2) {
    			ans = (k+1)/2;
    		}else {
    			ans = (k-cnt2);
    		}
    		
    		System.out.println(ans);
    	}
    	
    	sc.close();
    }
}
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值