HUST 1353 Dartboard

Description

A dartboard is a disc divided into  n segments, with each segment labeled with a distinct positive integer between 1 and  n. That integer indicates the score for hitting the segment. To make the game more interesting, the arrangement of numbers is chosen in such a way that the risk is maximized. The risk is estimated based on the differences in scores of adjacent segments. We're studying the following 'double-layered' structure of segments in this problem:   i.e.,  n is always even, and we split the disc into two layers of n/2 parts along the circumference. We enumerate the segments in the following manner: the first segment is some outer segment, the second segment is the corresponding inner segment, the third segment is some adjacent outer segment, etc. An example of this enumeration is shown on the picture above. The total risk is defined as the sum of squared differences between the scores of adjacent segments. If the value assigned to segment  i is  ai, then the risk is: R=∑  i=1 n(a  i-a  i+22+∑  i=1 n/2(a  2i-1-a  2i2 (we assume a  n+1=a  1 and a n+2=a  2 in this formula). You are to place the numbers from 1 through  n into the segments in such a way that the total risk  R is maximized.

Input

The input file contains an integer T means there are T cases followed. The next T lines: there is an even integer  n (6 ≤  n ≤ 100) in each line.

Output

Output the MAX(R);

Sample Input

2
10
6

Sample Output

461

87

这个可以通过观察发现内外圈的和就是n+1然后相邻的位置方向倒过来就可以了

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
int T, n, a[maxn], ans;

int sqr(int x, int y)
{
	return (x - y)*(x - y);
}

int main()
{
	cin >> T;
	while (T--)
	{
		cin >> n;
		int l = 1, r = n;
		ans = 0;
		a[0] = l;	a[1] = r;
		for (int q = 2, h = n - 2, i = 0; q <= h; q += 2, h -= 2, i++)
		{
			a[q] = --r; a[q ^ 1] = ++l;
			if (q < h) a[h] = --r, a[h ^ 1] = ++l;
			if (i & 1) {
				swap(a[q], a[q ^ 1]); if (q < h) swap(a[h], a[h ^ 1]);
			}
		}
		for (int i = 0; i < n; i++) ans += sqr(a[i], a[(i - 2 + n) % n]) + sqr(a[i], a[(i + 2) % n]) + sqr(a[i], a[i ^ 1]);
		printf("%d\n", ans / 2);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值