The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be teams participating in the contest, and the contest will be held on a huge round table with seats numbered from 1 to in clockwise order around it. The -th team will be seated on the -th seat.
BaoBao, an enthusiast for competitive programming, has made predictions of the contest result before the contest. Each prediction is in the form of , which means the -th team solves a problem during the -th time unit.
As we know, when a team solves a problem, a balloon will be rewarded to that team. The participants will be unhappy if the balloons take almost centuries to come. If a team solves a problem during the -th time unit, and the balloon is sent to them during the -th time unit, then the unhappiness of the team will increase by . In order to give out balloons timely, the organizers of the contest have bought a balloon robot.
At the beginning of the contest (that is to say, at the beginning of the 1st time unit), the robot will be put on the -th seat and begin to move around the table. If the robot moves past a team which has won themselves some balloons after the robot's last visit, it will give all the balloons they deserve to the team. During each unit of time, the following events will happen in order:
- The robot moves to the next seat. That is to say, if the robot is currently on the -th () seat, it will move to the ()-th seat; If the robot is currently on the -th seat, it will move to the 1st seat.
- The participants solve some problems according to BaoBao's prediction.
- The robot gives out balloons to the team seated on its current position if needed.
BaoBao is interested in minimizing the total unhappiness of all the teams. Your task is to select the starting position of the robot and calculate the minimum total unhappiness of all the teams according to BaoBao's predictions.
Input
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 three integers , and (, , ), indicating the number of participating teams, the number of seats and the number of predictions.
The second line contains integers (, and for all ), indicating the seat number of each team.
The following lines each contains two integers and (, ), indicating that the -th team solves a problem at time according to BaoBao's predictions.
It is guaranteed that neither the sum of nor the sum of over all test cases will exceed .
Output
For each test case output one integer, indicating the minimum total unhappiness of all the teams according to BaoBao's predictions.
解题思路:我们可以以第二个样例为基础模拟一下这个过程,第一个人坐在第一个位置上,他在1,2,3,4分钟分别过了一道题。第二个人坐在第二个位置上,他在第一分钟过了一道题。
所以我们假设机器人最开始在第一个位置上。
那么对第一个人的不开心值为 2,1,0,2 第二个人为 0
我们将机器人向前移动一个位置
我们发现不开心值变为了 1,0,2,1 第二个人为 2
即每个不开心值变为 (t - 1 + m) % m
这样我们就找到了一个递推关系,每次从上一个位置向这个位置转移的时候,所有提交的不开心值都减一,然后把变为负数的那些加上 m 即可。
我们很容易可以处理出来所有提交针对机器人处于位置1时候的不开心值,此时我们只需要枚举起点就可以了。
我们将所有提交的初始不开心值按照模m的余数分类。通过简单的证明 可以得到起点这能是让这些初始不开心值变为的0的点。可以画一个0-m的直线,上面散落一些初始不开心值,然后观察一下就会发现能让值最小的点一定在那些分割点上。
AC代码:
/*
* @Author: wchhlbt
* @Last Modified time: 2017-11-03
*/
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define AA first
#define BB second
#define ONES(x) __builtin_popcount(x)
#define _ << " " <<
using namespace std;
typedef pair<int, int> P;
typedef long long ll ;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const double eps =1e-8;
const int mod = 1000000007;
const double PI = acos(-1.0);
inline int read(){ int num; scanf("%d",&num); return num;}
const int maxn = 100025;
int pos[maxn];
ll d[maxn];
int main()
{
int t = read();
while(t--){
ll n,m,p;
scanf("%lld%lld%lld",&n,&m,&p);
for(int i = 1; i<=n; i++) pos[i] = read();
ll all = 0;
for(int i = 1; i<=p; i++){
ll x,t;
scanf("%lld%lld",&x,&t);
x = pos[x];
d[i] = ((x -1 -t)%m + m) % m ;
all += d[i];
}
ll ans = all;
sort(d+1,d+1+p);
for(int i = 1; i<=p; i++){
ans = min(ans,all + (i-1)*m - p*d[i]);
}
cout << ans << endl;
}
return 0;
}