列表

详情


NC214898. RiceArrangement

描述

Wowo is a hospitable Xinjiang uncle.  guests will have Uyghur Polo (a traditional Uyghur food) in Wowo's house around a big round table.  () chairs are placed around the table uniformly. Each guest sits on a chair and no two guests sit on the same chair.  bowls of Uyghur Polo are on the table. Each bowl is next to some chair (with or without some guest sitting on it). No two bowls locate at the same position. 



As a waiter, you are supposed to assign each person with exactly one bowl of Uyghur Polo. The table can be rotated, so each time you can turn it  degrees clockwise or counterclockwise. The bowls turn with the table while the chairs and guests do not move. When one bowl of Uyghur Polo is in front of a guest, he can either take it or wait for another.

You want to minimize the total times of table rotating so that everybody can have meals as quickly as possible.

(Formal definition: The boundary of the table is a circle.  chairs are at  points on the circle whose convex hull is a regular polygon with vertices. We name the points  in counterclockwise order. The -th bowl is at point b_i () initially. The -th guest is at point a_i () initially. If you turn the table counterclockwise, the bowl at point b_i () will be moved to point  after the rotation. If you turn the table clockwise, the bowl at point b_i () will be moved to point  after the rotation. ( is defined as the smallest nonnegative integer  such that  is a multiple of .)

输入描述

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  () indicating the size of the table and the number of persons and bowls of Uyghur Polo. 

In the second line, there are  integers  (), indicating the positions of the persons. No two guests share the same position.

In the third line, there are  integers  (), 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  over all test cases does not exceed .

输出描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 214ms, 内存消耗: 540K, 提交时间: 2021-01-17 13:50:13

#include<bits/stdc++.h>
using namespace std;

const int maxn=1005;
int T,n,m,ans;
int a[maxn],b[maxn],c[maxn];

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&m);
		ans=n;
		for(int i=0;i<m;i++)scanf("%d",&a[i]);
		for(int i=0;i<m;i++)scanf("%d",&b[i]);
		sort(a,a+m);
		sort(b,b+m);
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++)c[j]=(b[(i+j)%m]-a[j]+n)%n;
			sort(c,c+m);
			ans=min(ans,min(c[m-1],n-c[0]));
			for(int j=0;j<m-1;j++)
				ans=min(ans,c[j]+n-c[j+1]+min(c[j],n-c[j+1]));
		}
		printf("%d\n",ans);
	}
	return 0;
} 

上一题