Codeforces Round #194 (Div. 1) / 333B Chips(贪心+代码优化)

原创 2013年08月17日 22:27:57

B. Chips
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:

  • At least one of the chips at least once fell to the banned cell.
  • At least once two chips were on the same cell.
  • At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).

In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.

Input

The first line contains two space-separated integers n and m (2 ≤ n ≤ 10000 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct.

Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.

Output

Print a single integer — the maximum points Gerald can earn in this game.

Sample test(s)
input
3 1
2 2
output
0
input
3 0
output
1
input
4 3
3 1
3 2
3 3
output
1
Note

In the first test the answer equals zero as we can't put chips into the corner cells.

In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.

In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).



简单贪心,考虑相邻的两条边即可,详见代码。


完整代码:(未优化)

/*92ms,0KB*/

#include<cstdio>

bool isban[2][1000];

int main()
{
	int n, m, x, y, count = 0;
	scanf("%d%d", &n, &m);
	while (m--)
	{
		scanf("%d%d", &x, &y);
		isban[0][x] = true;
		isban[1][y] = true; ///只统计相邻的两条边就行
	}
	for (int i = 2; i < n; ++i)
	{
		if (!isban[0][i])
			count++;
		if (!isban[1][i])
			count++;
	}
	///在这种情况下两个chips(芯片/小木片)重合,上面多算了一次count
	if (n & 1 && !(isban[0][(n + 1) >> 1] || isban[1][(n + 1) >> 1]))
		count--;
	printf("%d", count);
	return 0;
}

优化版:

/*62ms,0KB*/

#include<cstdio>

bool isban[2][1000];

int main()
{
	int n, m, x, y;
	scanf("%d%d", &n, &m);
	int count = (n << 1) - 4;
	isban[0][1]=true;
	isban[0][n]=true;
	isban[1][1]=true;
	isban[1][n]=true;
	while (m--)
	{
		///只统计相邻的两条边就行
		scanf("%d%d", &x, &y);
		if (!isban[0][x])
		{
			--count;
			isban[0][x] = true;
		}
		if (!isban[1][y])
		{
			--count;
			isban[1][y] = true;
		}
	}
	///在这种情况下两个chips(芯片/小木片)重合,上面多算了一次count
	if (n & 1 && !(isban[0][(n + 1) >> 1] || isban[1][(n + 1) >> 1]))
		--count;
	printf("%d", count);
	return 0;
}



Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)A+B

A. Gotta Catch Em’ All!time limit per test:1 secondmemory limit per test:256 megabytesinput:standard...
  • qq_33183401
  • qq_33183401
  • 2017年01月16日 17:12
  • 243

Codeforces Round #254 (Div. 1)C. DZY Loves Colors(线段树经典操作/分块)

题目链接 C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 mega...
  • fouzhe
  • fouzhe
  • 2017年02月11日 15:17
  • 283

Codeforces Round #292 (Div. 1)

A. Drazil and Factorial 题意: F(x)定义为x各个位阶乘的乘积,给定一个数x要求求出F(x)=F(y)的最大的数y, 其中y的各个位不能包含0和1. 思路: 对于x的各个位,...
  • u013654696
  • u013654696
  • 2015年02月22日 13:35
  • 567

Codeforces Round #444 (Div. 2) D. Ratings and Reality Shows 贪心

http://codeforces.com/problemset/problem/887/D 题意:有一个模特,她有一个初始rating值 s。她每天可能进行拍照或show中的一种活动。每拍一次...
  • qq_36553623
  • qq_36553623
  • 2017年11月08日 23:47
  • 189

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作,都会拿走一个横行,一个竖行。 所以一共会操作min(横行,竖行)次。 #include #include #include #include ...
  • rowanhaoa
  • rowanhaoa
  • 2014年07月25日 14:39
  • 1481

Codeforces Round #172 (Div. 1) BMaximum Xor Secondary 单调栈

//给一个长度为N的个不相同的序列,找出所有区间中最大值和第二大数的异或值最大的值 //对于所有区间只需要找其最大值和第二大数,所以对于很多区间的结果是重复的 //对于每一个数,它起作用的区间只有...
  • cq_pf
  • cq_pf
  • 2015年05月26日 19:12
  • 708

【解题报告】Codeforces Round #401 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017年02月25日 17:55
  • 413

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) E. The Holmes Children

E. The Holmes Children time limit per test 2 seconds memory limit per test 256 megabytes...
  • mystery_guest
  • mystery_guest
  • 2017年03月04日 18:40
  • 286

Codeforces Round #315 (Div. 1)

Codeforces Round #315 (Div. 1)
  • u013023344
  • u013023344
  • 2015年08月16日 21:31
  • 1066

Codeforces Round #253 (Div. 1) (A, B, C)

Codeforces Round #253 (Div. 1) 题目链接 A:给定一些牌,然后现在要提示一些牌的信息,要求提示最少,使得所有牌可以被分辨出来。 思路:一共2^10种情况,直...
  • u011217342
  • u011217342
  • 2014年06月21日 21:54
  • 883
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #194 (Div. 1) / 333B Chips(贪心+代码优化)
举报原因:
原因补充:

(最多只允许输入30个字)