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


目录
相关文章
|
2月前
|
机器学习/深度学习
一篇文章讲明白hdu5698百度之星2016round2b第3题
一篇文章讲明白hdu5698百度之星2016round2b第3题
26 4
ACM刷题之路(十四)逆元取模 Travel along the Line
ACM刷题之路(十四)逆元取模 Travel along the Line
ACM刷题之路(二十四)HDU 2844 多重背包转换 Coins
ACM刷题之路(二十四)HDU 2844 多重背包转换 Coins
|
数据安全/隐私保护
【NOI】题目: 潜伏者(9分原因)
【NOI】题目: 潜伏者(9分原因)
227 0
【NOI】题目: 潜伏者(9分原因)
|
关系型数据库 MySQL 数据库
|
程序员
贤鱼的刷题日常--P1022 [NOIP2000 普及组] 计算器的改良--题目详解
🍀学习了解P1022 [NOIP2000 普及组] 计算器的改良
290 0
贤鱼的刷题日常--P1022 [NOIP2000 普及组] 计算器的改良--题目详解
|
开发工具
贤鱼的刷题日常-P1021 [NOIP1999 提高组] 邮票面值设计-题目详解
🍀学习了解P1021 [NOIP1999 提高组] 邮票面值设计
102 0
贤鱼的刷题日常-P1021 [NOIP1999 提高组] 邮票面值设计-题目详解