每日一题冲刺大厂第十六天提高组 codeforces 783 div1 Half Queen

大家好,我是泡泡,给大家带来每日一题的目的是为了更好的练习算法,我们的每日一题提高组是为了有余力的同学准备的,让大家练到各种各样的题目,一年以后,蜕变成为一个不一样的自己!

🎉欢迎关注🔎点赞👍收藏⭐️留言📝

❤️ :热爱C/C++与算法学习,云计算等,期待一起交流!

🙏作者水平有限,如果发现错误,求告知,多谢!

👻高校算法学习社区:https://bbs.csdn.net/forums/Suanfa

一起加入刷题内卷大军,还可以加入专属内卷群,里面福利多多大佬多多!

今日题目:3 div 1

You are given a board with nn rows and nn columns, numbered from 11 to nn. The intersection of the aa-th row and bb-th column is denoted by (a,b)(a,b).

A half-queen attacks cells in the same row, same column, and on one diagonal. More formally, a half-queen on (a,b)(a,b) attacks the cell (c,d)(c,d) if a=ca=c or b=db=d or a−b=c−da−b=c−d.

The blue cells are under attack.

What is the minimum number of half-queens that can be placed on that board so as to ensure that each square is attacked by at least one half-queen?

Construct an optimal solution.

Input

The first line contains a single integer nn (1≤n≤105) — the size of the board.

Output

In the first line print a single integer kk — the minimum number of half-queens.

In each of the next kk lines print two integers ai, bi (1≤ai,bi≤n) — the position of the ii-th half-queen.

If there are multiple solutions, print any. 

题目分析

题目难度:⭐️⭐️⭐️

题目涉及算法:数论,dp,暴力。

ps:有能力的小伙伴可以尝试优化自己的代码或者一题多解,这样能综合提升自己的算法能力

题解报告:

1.思路

解决无填充 对角线 斜线 挨着对角线的斜线,debug的过程中就AC了,可以等p佬完美题解。

2.代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int m = (n-1)*2/3+1;
	cout<<m<<endl;
	if(m&1)
	{
		int num = m/2,num1 = m-num;
		int sum = num1+1;
		for(int i=1;i<=num1;i++)
		{
			cout<<i<<" "<<sum-i<<endl;
		}
		sum = 2*m-num+1;
		for(int i=m;i>=m-num+1;i--)
		{
			cout<<i<<" "<<sum-i<<endl;
		}
	}
	else
	{
		int num = m/2,num1 = m-num+1;
		int sum = num1+1;
		for(int i=2;i<num1;i++)
		{
			cout<<i<<" "<<sum-i<<endl;
		}
		sum = 2*m-num+1;
		for(int i=m;i>=m-num+1;i--)
		{
			cout<<i<<" "<<sum-i<<endl;
		}
		cout<<1<<" "<<1<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloud、泡泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值