我是靠谱客的博主 感动雪碧,最近开发中收集的这篇文章主要介绍2020ICPC上海 H.Rice Arrangement,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目链接

题目描述
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,,n1 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 0bi<n) initially. The i {i} i-th guest is at point a i a_i ai ( 0 ≤ a i < n 0le a_i < n 0ai<n) initially. If you turn the table counterclockwise, the bowl at point b i b_i bi ( 1 ≤ i ≤ k 1le ile k 1ik) 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 1ik) will be moved to point ( b i − 1 )   m o d   n (b_i-1) bmod n (bi1)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} xr 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) 1n109,1kmin(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 0ai<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 0bi<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=(ajb(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(nc0,ck1)
  • 盘子整体往一个方向旋转一个角度,再反向旋转一个角度。
    选取 c c c 中相邻两个值 c j − 1 , c j c_{j-1},c_{j} cj1,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}) ncj+cj1+min(ncj,cj1)
    因为对于距离大于等于 c j c_j cj 的匹配,反向旋转 n − c j n-c_j ncj 一定能匹配到,另一方向旋转 c j − 1 c_{j-1} cj1 也能匹配到剩余部分,在两次旋转之间另外还要旋转到初始状态,即 min ⁡ ( n − c j , c j − 1 ) min(n-c_j,c_{j-1}) min(ncj,cj1) 。显然最方案只能存在于这些情况中。

枚举 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所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部