每日一题冲刺大厂第十六天提高组 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;
}


目录
相关文章
|
Java 应用服务中间件 Maven
解析Spring Boot中的Profile:配置文件与代码的双重掌控
解析Spring Boot中的Profile:配置文件与代码的双重掌控
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
2218 3
|
算法 定位技术
【逻辑设计】卡诺图 | 布尔方程式 | 最小项与最大项 | 卡诺图无关项 Don‘t cares
【逻辑设计】卡诺图 | 布尔方程式 | 最小项与最大项 | 卡诺图无关项 Don‘t cares
2075 0
|
TensorFlow 算法框架/工具
Pip命令更换国内源
【8月更文挑战第7天】Pip命令更换国内源。
1932 3
产品运营方法论问题之什么是北极星指标
产品运营方法论问题之什么是北极星指标
414 0
|
开发工具
二:使用VS2019 + .net 6创建 webapi 项目
0、创建一个.net 6项目。由于目前.net 6还是预览版,所以需要添加预览版SDK功能。工具 -> 选项 -> 环境 -> 预览功能 ,勾选使用.net sdk预览版。
1517 0
二:使用VS2019 + .net 6创建 webapi 项目
|
前端开发 JavaScript Java
怎么在云服务上启动java服务?(图文解说详细版)
怎么在云服务上启动java服务?(图文解说详细版)
怎么在云服务上启动java服务?(图文解说详细版)
|
NoSQL Java 程序员
大环境逐渐好转,Java 岗先爆了!大厂后端Java面试宝典合集需要吗
上半年大家听到“就业大环境很差”、“工作不好找”之类的太多了。如今大环境已经逐渐好转,虽然不需要太过焦虑,但是也要持续的提升自己。 最近和几位准备跳槽的小伙伴聊天发现,原有八股文已经不足以应对现在面试官的提问了。大厂对程序员的技术要求只会越来越高,谁掌握得更深、应用得更好,谁就更容易脱颖而出!
345 0
EMQ
|
存储 数据采集 Cloud Native
工厂生产数据实时分析,产品质量高效管控
EMQ基于云原生和云边协同的产品质量溯源解决方案,在现场设备端可实现数据的实时采集分析和异常报警,在云端实现工厂海量数据的统一接入存储,满足智慧工厂质量溯源需求。
EMQ
975 0
工厂生产数据实时分析,产品质量高效管控