比赛 noip 评测结果 AAAAAAWWWWWWWWWWWWWW
题目名称 __围栏问题 最终得分 30
用户昵称 goodqt 运行时间 0.083 s
代码语言 C++ 内存使用 0.28 MiB
提交时间 2016-11-04 21:41:33
显示代码纯文本
#include <cstdio>
#include <algorithm>

int m,k,n;
int data[1010][2];

int main(void)
{
	int a,b,c,d;
	int e,f,g,h;
	int i,j,k;
	int answer;
	
	freopen("xfence.in","r",stdin);
	freopen("xfence.out","w",stdout);
	
	scanf("%d %d %d",&m,&k,&n);
	
	for(i = 0;i < n;++i)
		scanf("%d %d",&data[i][0],&data[i][1]);
	
	if(k == 1){
		a = b = m + 1; 
		c = d = 0;
		for(i = 0;i < n;++i)
			a = std::min(a,data[i][0]),c = std::max(c,data[i][0] + 1),
			b = std::min(b,data[i][1]),d = std::max(d,data[i][1] + 1);
		printf("%d\n",2 * (c + d - a - b));
		return 0;
	}
	
	answer = 1e9;
	for(k = 0;k < (1 << n);++k)
	{
		a = b = 1e9; 
		c = d = -1e9;
		e = f = 1e9; 
		g = h = -1e9;
		for(i = 0;i < n;++i)
		{
			if(k & (1 << i)){
				a = std::min(a,data[i][0]),c = std::max(c,data[i][0] + 1),
				b = std::min(b,data[i][1]),d = std::max(d,data[i][1] + 1);
			}else{
				e = std::min(e,data[i][0]),g = std::max(g,data[i][0] + 1),
				f = std::min(f,data[i][1]),h = std::max(h,data[i][1] + 1);
			}
		}
		
		if(a == 1e9)
			a = b = c = d = 0;
		if(e == 1e9)
			e = f = g = h = 0;
		
		answer = std::min(answer,2 * (c + d - a - b) + 2 * (g + h - e - f));
	}
	
	printf("%d\n",answer);
}