ZOJ3981 Balloon Robot【模拟】

Balloon Robot

Time Limit: 1000 msMemory Limit: 65536 KB
The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be n teams participating in the contest, and the contest will be held on a huge round table with m seats numbered from 1 to in clockwise order around it. The -th team will be seated on the si-th seat.

BaoBao, an enthusiast for competitive programming, has made p predictions of the contest result before the contest. Each prediction is in the form of (ai, bi), which means the ai-th team solves a problem during the bi-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 ta-th time unit, and the balloon is sent to them during the tb-th time unit, then the unhappiness of the team will increase by tb-ta. 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 k-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:

1.The robot moves to the next seat. That is to say, if the robot is currently on the i-th (1<=i<m) seat, it will move to the (i+1)-th seat; If the robot is currently on the m-th seat, it will move to the 1st seat.
2.The participants solve some problems according to BaoBao’s prediction.
3.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 k 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 T, indicating the number of test cases. For each test case:

The first line contains three integers n, m and p (1<=n<105, n<=m<=109, 1<=p<=105), indicating the number of participating teams, the number of seats and the number of predictions.

The second line contains n integers s1,s2,…,sn (1<=si<=m, and si<>sj for all i<>j), indicating the seat number of each team.

The following p lines each contains two integers ai and bi (1<=ai<=n, 1<=bi<=109), indicating that the -th team solves a problem at time according to BaoBao’s predictions.

It is guaranteed that neither the sum of n nor the sum of p over all test cases will exceed 5×105.

Output
For each test case output one integer, indicating the minimum total unhappiness of all the teams according to BaoBao’s predictions.

Sample Input
4
2 3 3
1 2
1 1
2 1
1 4
2 3 5
1 2
1 1
2 1
1 2
1 3
1 4
3 7 5
3 5 7
1 5
2 1
3 3
1 5
2 5
2 100 2
1 51
1 500
2 1000
Sample Output
1
4
5
50

Hint
For the first sample test case, if we choose the starting position to be the 1st seat, the total unhappiness will be (3-1) + (1-1) + (6-4) = 4. If we choose the 2nd seat, the total unhappiness will be (2-1) + (3-1) + (5-4) = 4. If we choose the 3rd seat, the total unhappiness will be (1-1) + (2-1) + (4-4) = 1. So the answer is 1.

For the second sample test case, if we choose the starting position to be the 1st seat, the total unhappiness will be (3-1) + (1-1) + (3-2) + (3-3) + (6-4) = 5. If we choose the 2nd seat, the total unhappiness will be (2-1) + (3-1) + (2-2) + (5-3) + (5-4) = 6. If we choose the 3rd seat, the total unhappiness will be (1-1) + (2-1) + (4-2) + (4-3) + (4-4) = 4. So the answer is 4.

问题链接ZOJ3981 Balloon Robot
问题简述:(略)
问题分析:模拟题不解释。
程序说明:虽然给定的数不大(不超过32位),但是做乘法需要转为64位再计算,否则会溢出。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* ZOJ3981 Balloon Robot */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int N = 1e5 + 1;
int seat[N], d[N];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, m, p, a, b;
        scanf("%d%d%d", &n, &m, &p);
        for(int i = 0; i < n; i++) scanf("%d", &seat[i]);

        LL sum = 0;
        for(int i =0; i < p; i++) {
            scanf("%d%d", &a, &b);
            d[i] = (seat[a - 1] - 1 - b % m + m) % m;
            sum += d[i];
        }
        sort(d, d + p);

        LL ans = (LL)1e18;
        for(int i = 0; i < p; i++)
            if(i == 0 || d[i] != d[i - 1])
                ans = min(ans, sum + i * LL(m) - LL(p) * d[i]);

        printf("%lld\n", ans);
    }

    return 0;
}
原创文章 2323 获赞 2382 访问量 269万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105722574