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

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

今日题目: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.


14.png


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


目录
相关文章
|
监控 算法
独立成分分析(Independent Component Analysis,ICA)原理及代码实现
独立成分分析(Independent Component Analysis,ICA)原理及代码实现
独立成分分析(Independent Component Analysis,ICA)原理及代码实现
|
5月前
|
Ubuntu
Ubuntu系统重装:一步一步指南
本文介绍了如何重装Ubuntu系统,重装系统可以让电脑重新恢复到原始状态,从而解决电脑出现的各种问题,提高电脑的运行效率。重装系统的过程需要准备U盘,从官网下载Ubuntu系统,进入BIOS设置,根据提示进行安装,安装完成后重启电脑即可完成重装Ubuntu系统。
|
Linux Shell 调度
(三)Linux命令行工具和脚本编程:自动化任务和提高效率
Linux命令行工具和脚本编程是系统管理员和开发人员必备的技能。这些技能不仅可以自动化日常任务,还可以提高工作效率。本文将介绍如何使用Linux命令行工具和Shell脚本编程来自动化任务,并提供一些实用的技巧和示例。
420 1
|
算法 定位技术
【逻辑设计】卡诺图 | 布尔方程式 | 最小项与最大项 | 卡诺图无关项 Don‘t cares
【逻辑设计】卡诺图 | 布尔方程式 | 最小项与最大项 | 卡诺图无关项 Don‘t cares
1154 0
利用 GitHub Actions 自动化你的软件开发流程
在现代软件开发中,自动化是提升效率与质量的关键。GitHub Actions 作为 GitHub 的强大自动化工具,允许你在仓库中自动执行多种任务,如测试、打包、部署代码及自动合并 Pull Requests。本文介绍了 GitHub Actions 的核心概念、设置方法及其实用示例,帮助你快速上手并优化开发流程。通过 YAML 文件定义的工作流程可显著提高工作效率和代码质量。
|
Rust 开发工具 git
【一起学Rust】Rust包管理工具Cargo初步了解
【一起学Rust】Rust包管理工具Cargo初步了解
425 0
|
算法 决策智能
深度探讨回溯算法:追寻解空间的奇妙之旅
深度探讨回溯算法:追寻解空间的奇妙之旅
|
算法 区块链 vr&ar
共识算法-PBFT
简介 PBFT简介 BFT(Byzantine Fault Tolerance)是区块链共识算法中需要解决的一个核心问题。例如,公有链网络中,比特币和以太访中用的是POW,EOS用的是DPOS。PBFT一般用于联盟链场景中,它是共识节点较少的情况下BFT的一种解决方案。
3799 0
共识算法-PBFT
|
缓存 安全 前端开发
Flask新手教程(一)
Flask新手教程(一)
951 0