概述
题目链接
题目描述
The boundary of the table is a circle.
n
{n}
n chairs are at
n
{n}
n points on the circle whose convex hull is a regular polygon with
n
{n}
n vertices. We name the points
0
,
…
,
n
−
1
0,ldots, n-1
0,…,n−1 in counterclockwise order. The
i
{i}
i-th bowl is at point
b
i
b_i
bi (
0
≤
b
i
<
n
0le b_i<n
0≤bi<n) initially. The
i
{i}
i-th guest is at point
a
i
a_i
ai (
0
≤
a
i
<
n
0le a_i < n
0≤ai<n) initially. If you turn the table counterclockwise, the bowl at point
b
i
b_i
bi (
1
≤
i
≤
k
1le ile k
1≤i≤k) will be moved to point
(
b
i
+
1
)
m
o
d
n
(b_i+ 1) bmod n
(bi+1)modn after the rotation. If you turn the table clockwise, the bowl at point
b
i
b_i
bi (
1
≤
i
≤
k
1le ile k
1≤i≤k) will be moved to point
(
b
i
−
1
)
m
o
d
n
(b_i-1) bmod n
(bi−1)modn after the rotation. (
x
m
o
d
n
xbmod n
xmodn is defined as the smallest nonnegative integer
r
{r}
r such that
x
−
r
{x-r}
x−r is a multiple of
n
{n}
n.
输入描述:
There are multiple test cases. The first line of the input contains an integer
T
{T}
T, indicating the number of test cases. For each test case:
The first line contains two integers n , k {n,k} n,k ( 1 ≤ n ≤ 1 0 9 , 1 ≤ k ≤ min ( n , 1000 ) 1le n le 10^9,1 le k le min(n,1000) 1≤n≤109,1≤k≤min(n,1000)) indicating the size of the table and the number of persons and bowls of Uyghur Polo.
In the second line, there are k {k} k integers a 1 , a 2 , … , a k a_1,a_2,dots,a_k a1,a2,…,ak ( 0 ≤ a i < n 0 le a_i < n 0≤ai<n), indicating the positions of the persons. No two guests share the same position.
In the third line, there are k {k} k integers b 1 , b 2 , … , b k b_1,b_2,dots,b_k b1,b2,…,bk ( 0 ≤ b i < n 0 le b_i < n 0≤bi<n), indicating the initial positions of the bowls. No two bowls of Uyghur Polo locate at the same position.
It is guaranteed that the sum of k {k} k over all test cases does not exceed 5000 {5000} 5000.
输出描述:
For each test case, output the minimal total times of rotations such that each guest can have exactly one bowl of Uyghur Polo.
示例1
输入
1
4 2
0 3
1 2
输出
2
示例2
输入
1
14 5
0 12 13 8 9
9 2 6 13 5
输出
6
将序列
a
a
a 和
b
b
b 排序。
记
c
i
,
j
=
(
a
j
−
b
(
j
+
i
)
m
o
d
k
+
n
)
m
o
d
n
c_{i,j}=(a_j-b_{(j+i)bmod k}+n)bmod n
ci,j=(aj−b(j+i)modk+n)modn,然后将序列
c
c
c 排序。
显然,最优方案最存在于以下两种方案中:
- 第
j
j
j 个盘子匹配第
(
j
+
i
)
m
o
d
k
(j+i)bmod k
(j+i)modk 个盘子,即盘子整体往一个方向旋转。
答案为两个方向中的最小值,即 min ( n − c 0 , c k − 1 ) min(n-c_0,c_{k-1}) min(n−c0,ck−1) 。 - 盘子整体往一个方向旋转一个角度,再反向旋转一个角度。
选取 c c c 中相邻两个值 c j − 1 , c j c_{j-1},c_{j} cj−1,cj ,则答案为 n − c j + c j − 1 + min ( n − c j , c j − 1 ) n-c_j+c_{j-1}+min(n-c_j,c_{j-1}) n−cj+cj−1+min(n−cj,cj−1) 。
因为对于距离大于等于 c j c_j cj 的匹配,反向旋转 n − c j n-c_j n−cj 一定能匹配到,另一方向旋转 c j − 1 c_{j-1} cj−1 也能匹配到剩余部分,在两次旋转之间另外还要旋转到初始状态,即 min ( n − c j , c j − 1 ) min(n-c_j,c_{j-1}) min(n−cj,cj−1) 。显然最方案只能存在于这些情况中。
枚举 i i i 求解,时间复杂度为 O ( T k 2 log k ) O(Tk^2log k) O(Tk2logk) 。
#include<bits/stdc++.h>
using namespace std;
const int N = 1005;
int a[N], b[N], c[N];
int T, n, k;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++)scanf("%d", &a[i]);
for (int i = 0; i < k; i++)scanf("%d", &b[i]);
sort(a, a + k), sort(b, b + k);
int ans = n;
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++)
c[j] = (a[j] - b[(j + i) % k] + n) % n;
sort(c, c + k);
ans = min(ans, min(c[k - 1], n - c[0]));
for (int j = 1; j < k; j++)
ans = min(ans, n - c[j] + c[j - 1] + min(n - c[j], c[j - 1]));
}
printf("%dn", ans);
}
}
最后
以上就是感动雪碧为你收集整理的2020ICPC上海 H.Rice Arrangement的全部内容,希望文章能够帮你解决2020ICPC上海 H.Rice Arrangement所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复