ZOJ 3981 Balloon Robot (思维+推理)

Balloon Robot


Time Limit: 1 Second      Memory Limit: 65536 KB


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:

  1. 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.
  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  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只队伍编号1~n(n<=1e5),给出它们的座位编号,座位共有m(n<=m<=1e9)个围成一圈。q(q<=1e5)次AC

每次AC 为 x y表示队伍编号x的队伍在第y秒AC的。

有一个机器人负责送气球。每秒会自动向右移动一个单位,如果这个座位所在队伍在到达这里之前AC过题目,就会给他们足够的气球。假设第i个队伍在第j秒AC一道题,而气球在第k秒送达,则这个队伍的不满意度为k-j。

你需要确定一个起点,使所有队伍都获得相应的气球之后,总不满意度最小。

输入这个最小值。

思路:感觉说难不难,说简单也不简单。算是思维题吧。

考虑到数据范围为1e5,我们只能使用O(nlogn)以下的算法。

首先想到的就是排序贪心。

假设第1秒机器人在1号座位,则每一次AC都可以求出一个不满意度t[i],因此求出初始不满意度总和sum。

注意如果第1秒1号座位AC了一道题,机器人是送不了气球的,因为它正在赶往2号的途中。(由题意知)

然后对于每次AC的不满意度从小到大排序,发现把第i次AC当做送出的第一个气球,sum变化为

sum-(q-i)*t[i]+i*(m-t[i])=

sum-q*t[i]+i*m     (i=0~q)

然后就不需要什么贪心了,直接排序求即可。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
using namespace std;
const int maxn=200010;
ll n,m,q;
ll a[maxn],t[maxn];
ll c[maxn];
ll ans,ct,cnt,tmp,flag,sum;
char s[maxn];
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld%lld",&n,&m,&q);
        ans=0;sum=0;
        for(int i=0;i<n;i++)
        scanf("%lld",&a[i]);
        for(int i=0;i<q;i++)
        {
            ll x,y;
            scanf("%lld%lld",&x,&y);
            t[i]=(a[x-1]-1-y+m)%m;
            sum+=t[i];
        }
        sort(t,t+q);
        ans=inf;
        //sum-(q-i)*t[i]+i*(m-t[i]);
        for(int i=0;i<q;i++)
        {
            ans=min(ans,sum-t[i]*q+i*m);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值