Balloon Robot ZOJ - 3981(思维)

Balloon Robot ZOJ - 3981

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.
he 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.
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.

题意:

第一行三个数字n, m, q表示有m个座位围成一个环,n个队伍,q次A题

接下来n个数表示n个队伍所在位置(1<=ai<=m)

再接下来q行,每行a, b表示第a个队伍在第b秒A了一道题

有一个只会每一秒顺时针移动一个位置的发气球机器人

只要当前队伍有题目已经A了就会给他对应数量的气球(当然每道题最多1个气球)

如果a队伍在b时刻A了一道题,并在c时刻才拿到气球,那么这个队伍就会积累c-b点不开心值

求一个机器人起始位置(一开始是第0秒)使得所有队伍最终不开心值之和最小

分析:

假设我们起始位置是1,p为当前队伍所在下标,b为该队伍AC时间,所以得到该队伍不开心值的公式:w=(p-1-(b%m)+m)%m

为什么是这个公式呢,我们想,机器人是在这个圈上转,在某一时刻,有个队A了一个题,那么机器人的位置只有两种情况,1、还没有到A题队伍的位置,那么也就是说,A题队伍在机器人前面,那么此时p-1 >= b % m,为什么这里-1呢,因为机器人从1位置开始走的的话,到达i位置时实际是第i-1时刻到达的,所以减1,此时中间的间隔时间就是用p-1 - b%m就行了
2、如果此时机器人已经过了A题队伍的位置,它需要绕一圈后再回到A题队伍位置,此时很明显p-1-b%m是负数,那么我们加上m实际就上另一半所花费时间就是中间时间间隔。

为什么要模m因为就像上面说的,机器人只有两种位置,在A题队伍前或后,所以我们只需要知道其相对位置即可

我们算出的是机器人从位置1每个人的不开心值,然后排个序,这样枚举每个A题时间,那么当枚举到这个点的时候这个点肯定不开心值为0,即减去了w[i],同样它后面的点也要减去w[i],但是它前面的点,减的话就成了负数我们在加上m,这样就是原始总的值+i*m-q*w[i]
不断更新答案。

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
ll a[maxn],w[maxn];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ll n,m,q;
        scanf("%lld%lld%lld",&n,&m,&q);
        ll sum = 0;
        for(int i = 1; i <= n; i++){
            scanf("%lld",&a[i]);
        }
        for(int i = 0; i < q; i++){
            ll x,y;
            scanf("%lld%lld",&x,&y);
            w[i] = (a[x] - 1 - (y % m) + m) % m;
            sum += w[i];
        }
        sort(w,w+q);
        ll ans = 1e18;
        for(int i = 0; i < q; i++){
            ans = min(ans,(sum + i * m - q * w[i]));
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/82797580