NC214898. RiceArrangement
描述
输入描述
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; }