链接:https://www.nowcoder.com/acm/contest/140/I
来源:牛客网

White Cloud has a square of n*n from (1,1) to (n,n).
White Rabbit wants to put in several cars. Each car will start moving at the same time and move from one side of one row or one line to the other. All cars have the same speed. If two cars arrive at the same time and the same position in a grid or meet in a straight line, both cars will be damaged.
White Cloud will destroy the square m times. In each step White Cloud will destroy one grid of the square(It will break all m grids before cars start).Any car will break when it enters a damaged grid.

White Rabbit wants to know the maximum number of cars that can be put into to ensure that there is a way that allows all cars to perform their entire journey without damage.
(update: all cars should start at the edge of the square and go towards another side, cars which start at the corner can choose either of the two directions)
For example, in a 5*5 square
legal
illegal(These two cars will collide at (4,4))
illegal (One car will go into a damaged grid)

输入描述:

The first line of input contains two integers n and m(n <= 100000,m <= 100000)
For the next m lines,each line contains two integers x,y(1 <= x,y <= n), denoting the grid which is damaged by White Cloud.

输出描述:

Print a number,denoting the maximum number of cars White Rabbit can put into.

输入例子:
2 0
输出例子:
4

-->

示例1

输入

复制

2 0

输出

复制

4

备注:

分析:首先看没有陷阱的时候,n*n最多可以在边界上放几辆车,然后再思考加每个陷阱被影响到的行和列。

通过爆搜或者手写所有情况找规律,我们很容易得出n*n的时候边界放的车的奇数的情况为:(n/2)*4+1,偶数的情况为:2*n

每次加陷阱会影响到陷阱所在的行和列,我们分别用两个vis数组保存起来被影响到的行和列。

首先看n为偶数

图为n是偶数的时候,此时n为4,最多可以放车子八辆,八辆车的情况我们可以变成图中这种情况(比如(2,2)我可以直接横移到(2,1)边界处就满足了条件)

然后我们可以发现陷阱处于此图中任何位置都会影响到两辆车,所以n为偶数时候陷阱的位置行和列都将受到影响,我们记录下来就好

接着是n为奇数

图为n为5的时候,最多可以放九辆车,如上面一样可以画成这样的形式。然后我们很容易看出来除了陷阱位于中心点,在其他点都可以影响到两辆车,中心点影响到一辆车。

下面是我的实现代码

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 10000007;
typedef long long ll;
int vis1[maxn], vis2[maxn] ;
struct node{
int x, y ;
}e[maxn];
int n , m ;
int main(){
while( scanf("%d %d",&n,&m) != EOF) {
memset(vis1,0,sizeof(vis1)) ;
memset(vis2,0,sizeof(vis2)) ;
long long int sum = n%2 == 0 ? 2*n : (n/2)*4 + 1 ;
for( int i = 1 ; i <= m ; i ++ ){
scanf("%d %d",&e[i].x,&e[i].y) ;
vis1[e[i].x] = 1 ; vis2[e[i].y] = 1 ;
}
long long ans = 0 ;
for(int i = 1 ; i <= n ; i ++) {
if(vis1[i]) ans ++ ;
if(vis2[i]) ans ++ ;
}
if( n%2 && ( vis1[(n + 1)/2] || vis2[(n + 1)/2] ) ) {
ans -- ;
}
printf("%lld\n",sum - ans) ;
}
return 0 ;
}

  

牛客网暑期ACM多校训练营(第二场) I Car 思维的更多相关文章

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  7. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  8. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  9. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  10. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

随机推荐

  1. MySQL中一些关于索引的知识点

    什么是索引 索引是一种数据结构,其作用就是用来提高数据查询效率.比较常用的比喻就是将其类比为书籍的目录.通过目录可以精确的找到某一章节的内容所在页. 在数据量较小的时候使用索引其实也没有什么意义,即使 ...

  2. java遍历所有目录和文件

    package xian; import java.io.File; import java.util.ArrayList; public class GetFile { private static ...

  3. RocketMQ中Broker的HA策略源码分析

    Broker的HA策略分为两部分①同步元数据②同步消息数据 同步元数据 在Slave启动时,会启动一个定时任务用来从master同步元数据 if (role == BrokerRole.SLAVE) ...

  4. Extjs4 combobox autoLoad: true 时,加载两次

    问题是这样的,combobox 远程加载数据时,当我们把 store 设置为  autoLoad: tue, 时,这样页面加载时,store 会load 一次,但是我们在第一次点击 下来框时,他还会 ...

  5. 【Java笔记】【Java核心技术卷1】chapter3 D3数据类型

    package chapter3; public class D3数据类型 { public static void main(String[] arg) { //Java 整型(字节数不会随硬件变化 ...

  6. 【杂项】关于NOIP2018复赛若干巧合的声明

    导言 参加NOIP2018时本人学龄只有两个月,却斩获了省一等奖,保送了重点中学,这看上去是个我创造的神话,然而,在我自己心中,我认为这只是个巧合(其实我认为运气也是实力的一部分),接下来,我将说明一 ...

  7. 安装VMware14虚拟机,centos7版本的linux 软件地址

    首先下载虚拟机软件和centos7的linux系统的镜像软件系统, https://pan.baidu.com/s/1cJfzpaLwB4dfe2W8gGEAPQ 两个文件 非常好用 虚拟机安装 很简 ...

  8. 容易上手搭建vue2.0开发环境

    第一步:安装node 前端开发框架和环境都是需要 Node.js ,先安装node.js开发环境,vue的运行是要依赖于node的npm的管理工具来实现,下载https://nodejs.org/en ...

  9. 《机器学习基石》---Linear Models for Classification

    1 用回归来做分类 到目前为止,我们学习了线性分类,线性回归,逻辑回归这三种模型.以下是它们的pointwise损失函数对比(为了更容易对比,都把它们写作s和y的函数,s是wTx,表示线性打分的分数) ...

  10. C#_会员管理系统

    https://www.cnblogs.com/start-from-scratch/p/5420588.html