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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章