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=1n(a i-a i+2) 2+∑ i=1n/2(a 2i-1-a 2i) 2 (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 6Sample Output
46187
这个可以通过观察发现内外圈的和就是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;
}
标签:risk,1353,Dartboard,segments,HUST,int,integer,include,segment From: https://blog.51cto.com/u_15870896/5838871