B. Numbers on the Chessboard -codeforces1027 -csdn博客

You are given a chessboard of size n×n. It is filled with numbers from 1 to n2 in the following way: the first ⌈n22⌉ numbers from 1 to ⌈n22⌉ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n2−⌈n22⌉ numbers from ⌈n22⌉+1 to n2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation ⌈xy⌉ means division x by y rounded up.

For example, the left board on the following picture is the chessboard which is given for n=4 and the right board is the chessboard which is given for n=5.

You are given q queries. The i-th query is described as a pair xi,yi. The answer to the i-th query is the number written in the cell xi,yi (xi is the row, yi is the column). Rows and columns are numbered from 1 to n.

Input
The first line contains two integers n and q (1≤n≤109, 1≤q≤105) — the size of the board and the number of queries.

The next q lines contain two integers each. The i-th line contains two integers xi,yi (1≤xi,yi≤n) — description of the i-th query.

Output
For each query from 1 to q print the answer to this query. The answer to the i-th query is the number written in the cell xi,yi (xi is the row, yi is the column). Rows and columns are numbered from 1 to n. Queries are numbered from 1 to q in order of the input.
Examples
inputCopy
4 5
1 1
4 4
4 3
3 2
2 4
outputCopy
1
8
16
13
4
inputCopy
5 4
2 1
4 2
3 3
3 4
outputCopy
16
9
7
20
Note
Answers to the queries from examples are on the board in the picture from the problem statement.

  • 题意:给你一个n×n的方框,在里面根据规则填数字,有q个查询,输出每个查询x,y对应位置数字大小。
    规则:行和列之和为偶数是,数字为前n^2/2(上限),顺序从左到右,从上倒下,如果为奇数,则为n^2/2+1到n^2;

  • 题解:首先判断行和列之和奇偶行,如果是奇数,那么加上n^2/2(如果n^2为奇数,那么要加上1)。然后就是你恶不的判断了。我们首先可以判定如果行满足是两行连续时,那么一共有n个数填了进去,因此我用(a-1)/2看看满足的有多少个满两行对。具体操作如下。
    关于这个题的解法大家可以自己画图试一试,找一找为什么要这样写

#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define mp make_pair
#define _ ios::sync_with_stdio(false)
bool SUBMIT = 1;
typedef long long ll;
using namespace std;
const double PI = acos(-1);
ll n,q;
int main()
{if(!SUBMIT)freopen("i.txt","r",stdin);else _;cin>>n>>q;for(ll i=0;i<q;i++){ll a,b;cin>>a>>b;ll c;if(n*n%2)c=n*n/2+1;else c=n*n/2;ll ans=0;if(a>2)ans+=n*((a-1)/2);a-=(a-1)/2*2;if(a>1)ans+=n/2;if((a+b)%2){//if(a>1&&n%2)ans++;//此时不用这句话,因为奇数的话加在了前n^2/2内ans+=(b+1)/2;ans+=c;}else{if(a>1&&n%2)ans++;//必须有,n为奇数且前半部分的话,每对第一行还要多一个数ans+=(b+1)/2;}cout<<ans<<endl;}return 0;
}

欢迎欢迎,如果大家喜欢的话可以关注一波O(∩_∩)O哈哈~

posted @ 2018-08-19 13:51 i-Curve 阅读(...) 评论(...) 编辑 收藏

B. Numbers on the Chessboard -codeforces1027 -csdn博客相关推荐

  1. taoqick 搜索自己CSDN博客

    L1 L2正则化和优化器的weight_decay参数 kaiming初始化的推导 Pytorch动态计算图 Pytorch自动微分机制 PyTorch中在反向传播前为什么要手动将梯度清零? 通俗讲解 ...

  2. 关于博客园与CSDN博客同步的说明

    把博客园与CSDN比较,我认为博客园有技术内容优势,CSDN有行业资源优势,两者都比较重要.因此在两家网站同步发布博客. CSDN博客地址:http://blog.csdn.net/caoshiyin ...

  3. 2019年度CSDN博客之星TOP10榜单揭晓,你上榜了吗?

    培根说,『读书造成充实的人,会议造成未能觉悟的人,写作造成正确的人』. 在短信短视频快速迭代的快时代,更深度的思考.更正确的实践,更成体系的写作与分享,尤显可贵.这里,每一篇博文都是开发者实战的经验解 ...

  4. CSDN博客的创建及使用

    文章目录 一.创建CSDN博客详细流程 1. 注册 2. 绑定手机 二.编辑和发布博客 1. 设置markdown格式 2. 发布文章 3. 常见的markdown语法 3.1 分级标题 3.2 目录 ...

  5. 这是一名南京985AI硕士,CSDN博客专家

    微信公众号推荐 AI蜗牛车公众号 微信公众号<AI蜗牛车>,公众号致力于技术项目化,具体化,思考化,会写系列的项目工程文章,细致到位,也会写一个读物的读书笔记,或者一个语言/框架的学习笔记 ...

  6. CSDN博客获取积分规则!不是下载积分!如何快速增长积分!

    博客积分是CSDN对用户努力的认可和奖励,也是衡量博客水平的重要标准. 博客等级也将由博客积分唯一决定.积分规则具体如下: 1.每发布一篇原创或者翻译文章:可获得10分: 2.每发布一篇转载文章:可获 ...

  7. 类选择器和所作用的标签一起写为什么不起作用? - CSDN博客

    原文:类选择器和所作用的标签一起写为什么不起作用? - CSDN博客 HTML代码: css样式: 这不是将样式作用于circle类下的有current类的li标签吗?为什么不起作用? 原因: 选择器 ...

  8. microsoft edge 打不开 csdn 博客

    microsoft edge 打不开 csdn 博客

  9. 如何转载别人的csdn博客

    起意 看到一篇不错的博客,想转载但是不会(没有转载键),网搜发现没有Chrome浏览器的详细转载办法,受博主Bily猪启发,弄了这个教程,给小白的,大神勿嗤.时间紧急的直接看总结. 实践 检查.在喜欢 ...

最新文章

  1. POJ - 3538 - Domestic Networks
  2. vs2015 QT5.6 兼容xp系统
  3. 定制AjaxControlToolkit:给CalendarExtender增加清除功能
  4. excel合并计算_【Excel】合并计算和模拟分析的应用
  5. 其他综合-跳板机jumpserver的安装
  6. mysql 转换编码
  7. 大型情感剧集Selenium:6_selenium中的免密登陆与cookie操作 #华为云·寻找黑马程序员#
  8. python文件读写方法手机,python文本文件读写的3种方法
  9. 基础计算机构,基础计算与设计
  10. 大型综合体弱电智能化解决方案标书
  11. verilog学习记(加法器)
  12. pyqt5 python2.7_python2.7 安装pyqt5
  13. Storm入门学习随记
  14. 吉林大学操作系统概论
  15. 常用的140个Windows XP设置 [转]
  16. java adobe pdf转word_adobe reader如何把PDF转换成word?adobe reader把PDF转换成word教程
  17. 说说 褥羊毛和薅羊毛的那些事
  18. Redis 持久化,写入磁盘的方式
  19. 文科类文献综述怎么写?
  20. Mosquitto 安装指南

热门文章

  1. 激光测距VL53L0X中断模式 源代码 已测试
  2. Unit 3, Playback Buffer
  3. 2022-2028全球自动车道警告系统行业调研及趋势分析报告
  4. Netty之ChannelHandlerContext.writeAndFlush与channel.writeAndFlush的区别
  5. 91---Python 直角坐标系下绘制双曲线图像
  6. GitHub学生认证,可以愉快的使用Copilot了
  7. gnss、rtk定位原理
  8. OFFICE使用技巧PDF文件
  9. 平板电脑必装十大软件_【电脑必装软件】5个不火实用的技巧,每一个都很强势!...
  10. 怎样才能学好一门编程语言?要怎样去学习才算成功?成为一名合格的程序员