每日一题冲刺大厂第十六天提高组 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月前
|
C++
【PTA】​L1-062 幸运彩票 ​ (C++)
【PTA】​L1-062 幸运彩票 ​ (C++)
46 0
【PTA】​L1-062 幸运彩票 ​ (C++)
PTA--天梯赛题集题解--打印沙漏
PTA--天梯赛题集题解--打印沙漏
PTA 7-2 找奇葩 (20 分)
在一个长度为 n 的正整数序列中,所有的奇数都出现了偶数次,只有一个奇葩奇数出现了奇数次。你的任务就是找出这个奇葩。
78 0
|
8月前
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
50 0
|
9月前
|
Java
ACM刷题之路(二十四)HDU 2844 多重背包转换 Coins
ACM刷题之路(二十四)HDU 2844 多重背包转换 Coins
|
Java Shell
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
135 0
贤鱼的刷题日常--P1019 [NOIP2000 提高组] 单词接龙--题目详解
🍀学习了解P1019 [NOIP2000 提高组]单词接龙--题目详解
88 0
贤鱼的刷题日常--P1019 [NOIP2000 提高组] 单词接龙--题目详解
|
关系型数据库 MySQL 数据库

热门文章

最新文章