【题解】CF1929B Sasha and the Drawing

CF1929B

题意

给定一个 \(n\times n\) 的正方形,已知正方形最多有 \(4\times n-2\) 条对角线,要求要有至少 \(k\) 条对角线经过至少一块黑色方格,求至少要将几条对角线涂成黑色。

分析

分类讨论:

  • \(k<=4\times n-4\) 时,就只需要在上下两侧图就行,所以答案是 \([\frac{k}{2}]\)
  • \(k>4\times n-4\) 时,则答案是 \(k-2\times n+2\)

\(n==4,k==12\) 时:

代码

#include<bits/stdc++.h>
using namespace std;
int t,n,k;
int main(){
	cin>>t;
	while(t--){
		cin>>n>>k;
		if(k<=4*n-4)cout<<(k+1)/2;//千万不能写ceil(k*1.0)/2 
		else cout<<k-2*n+2;
		cout<<"\n";
	}
	return 0;
}
posted @ 2024-04-06 17:58  Kcjhfqr  阅读(25)  评论(0)    收藏  举报
.poem-wrap { position: relative; width: 1000px; max-width: 80%; border: 2px solid #797979; border-top: none; text-align: center; margin: 40px auto; } .poem-left { left: 0; } .poem-right { right: 0; } .poem-border { position: absolute; height: 2px; width: 27%; background-color: #797979; } .poem-wrap p { width: 70%; margin: auto; line-height: 30px; color: #797979; } .poem-wrap h1 { position: relative; margin-top: -20px; display: inline-block; letter-spacing: 4px; color: #797979; font-size: 2em; margin-bottom: 20px; } #poem_sentence { font-size: 25px; } #poem_info { font-size: 15px; margin: 15px auto; }