【Codeforces Round 262 (Div 2)E】【爆搜 估价函数剪枝 搜索步长剪枝】Roland and Rose 整格放点 点对欧几里得距离平方和尽可能大

Roland and Rose
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Roland loves growing flowers. He has recently grown a beautiful rose at point (0, 0) of the Cartesian coordinate system. The rose is so beautiful that Roland is afraid that the evil forces can try and steal it.

To protect the rose, Roland wants to build n watch towers. Let's assume that a tower is a point on the plane at the distance of at most rfrom the rose. Besides, Roland assumes that the towers should be built at points with integer coordinates and the sum of squares of distances between all pairs of towers must be as large as possible. Note, that Roland may build several towers at the same point, also he may build some of them at point (0, 0).

Help Roland build the towers at the integer points so that the sum of squares of distances between all towers is maximum possible. Note that the distance in this problem is defined as the Euclidian distance between points.

Input

The first line contains two integers, n and r (2 ≤ n ≤ 8; 1 ≤ r ≤ 30).

Output

In the first line print an integer — the maximum possible sum of squared distances. In the i-th of the following n lines print two integers,xi, yi — the coordinates of the i-th tower. Each tower must be inside or on the border of the circle with radius r. Note that there may be several towers located at the same point of the plane, also some towers can be located at point (0, 0).

If there are multiple valid optimal arrangements, choose any of them.

Examples
input
4 1
output
16
0 1
0 1
0 -1
0 -1
input
3 6
output
312
0 6
5 -3
-5 -3

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, r;
struct Point
{
	int x, y;
	double dis;
	bool operator < (const Point&b)const
	{
		return dis > b.dis;
	}
}P[3600]; int pnum;
int d[405][405];
void init()
{
	pnum = 0;
	for (int i = -30; i <= 30; ++i)
	{
		for (int j = -30; j <= 30; ++j)
		{
			double dis = sqrt(i*i + j*j);
			if (dis > r - 1 && dis <= r)
			{
				++pnum;
				P[pnum].x = i;
				P[pnum].y = j;
				P[pnum].dis = dis;
			}
		}
	}
	sort(P + 1, P + pnum + 1);
	//++pnum; P[pnum].x = 0; P[pnum].y = 0;
	for (int i = 1; i <= pnum; ++i)
	{
		for (int j = i; j <= pnum; ++j)
		{
			d[i][j] = (P[i].x - P[j].x)*(P[i].x - P[j].x)
					+ (P[i].y - P[j].y)*(P[i].y - P[j].y);
			d[j][i] = d[i][j];
		}
	}
}
int R; int len;
int a[10], A[10];
int ans;
void dfs(int p,int num,int dis)
{
	if (num == n)
	{
		if (dis <= ans)return;
		ans = dis;
		MC(A, a);
		return;
	}
	if (p > pnum)return;
	//int more = (n - num)*num + (n - num)*(n - num - 1) / 2;
	//if (dis + more*R <= ans)return;

	int top = min(pnum, p + len);
	for (int i = p; i <= top; ++i)
	{
		int tmp = dis; 
		for (int j = 1; j <= num; ++j)tmp += d[i][a[j]];
		double more = 1.01*tmp / num*(n-1)*(n - num);
		if (dis + more < ans)continue;
		a[num + 1] = i; dfs(i, num + 1, tmp);
	}
}
int main()
{
	while (~scanf("%d%d", &n, &r))
	{
		R = (r + r)*(r + r); len = 10;
		init();
		ans = 0;
		dfs(1, 0, 0);
		printf("%d\n", ans);
		for (int i = 1; i <= n; ++i)
		{
			printf("%d %d\n", P[A[i]].x, P[A[i]].y);
		}
	}
	return 0;
}
/*
【trick&&吐槽】
1,压缩搜索步长是个剪枝的好技巧
2,剪枝的估价函数要好好设计。
3,这题不能按照过程值的优劣做剪枝,因为是错误的。
4,我们并不需要考虑原点。

【题意】
给你一个网格图,我们只在整点放棋子。
有n(2<=n<=8)个点,它们距离原点的最远距离为r(1<=r<=30)
让你求出一个点集。使得两两之间的距离尽可能远。

【类型】
贪心+爆搜

【分析】
猜想:我们只要贪心,选择的点的范围在[r-1,r]之间即可。
然而,这些点的数量依然很多,直接爆搜还是很可怕。
这里运用贪心猜点位置,+估价函数剪枝和搜索步长剪枝

*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值