ZOJ

来源:互联网 发布:软件采购制度 编辑:程序博客网 时间:2024/06/10 13:06

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

42 3 31 21 12 11 42 3 51 21 12 11 21 31 43 7 53 5 71 52 13 31 52 52 100 21 511 5002 1000

Sample Output

14550

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.



题意:这道题难就难在你要读懂题意并抽象出模型。我的理解就是,有一圈桌子,桌子在特定时间会做出一道题。有一个机器人,每次走一个桌子,如果走到该桌子,该桌子做出了一道题,就给他一个气球,这个时候这个桌子会产生一个怒气值,为做出题的时间和收到气球的时间的差值。问题为,你可以让机器人在任意一个桌子开始行走,求所有桌子的最少的怒气值和。


解题思路:一开始毫无思路,但是只要分析并抽象出模型后,就是一道简单的思维题了。每个人抽象出来的都不一样。我想到的就是,桌子做出了一道题,实际上就是给这个桌子榜一个气球,然后气球有一个高度,高度就是他做出题的时间。然后有一个机器人,机器人每次往右上走一格,然后把下面的气球收掉,并产生怒气值。然后不断地走。由于是绕圈,有点难想象。如图。所有气球与它上面的第一条线的距离的和就是怒气值和。




如果看到这里能理解的话,就可以往下看。这里我们可以简化一下,所有直线拼成一条直线,气球的横坐标挪一下就好了,实际上就是循环。




每走一个桌子花费是一秒,所以这条机器人的路径斜率为1 。最后问题的答案就是找到一条斜率为1的直线,使得所有气球离那条直线的距离(y0-y1)的和最小。如果看到这里能懂的话,就应该没问题了。由于P很小,所以我们可以直接枚举所有气球,枚举完后要把那个气球往下移,这样才能计算出正确的值。计算前,我们要对气球排序,这样才能通过上一次的答案算出这一次的答案,然后复杂度就降低了! 



好吧……有点难理解,希望能懂。


#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>using namespace std;typedef long long int ll;struct point{    ll x;    ll y;    point(ll a=0,ll b=0){        x=a;        y=b;    }}list[100005];bool cmp(point a,point b){    if(a.y-a.x==b.y-b.x)        return a.y>b.y;    else        return a.y-a.x>b.y-b.x;}ll bb[100005];int main(){    int t;    scanf("%d",&t);    while(t--){        ll team,seat,ball;        scanf("%lld%lld%lld",&team,&seat,&ball);                map<ll,ll> TtoS;//把队伍编号映射到座位        for(ll i=0;i<team;i++){            ll temp;            scanf("%lld",&temp);            TtoS[i+1]=temp;        }                ll a,b;        for(ll i=0;i<ball;i++){            scanf("%lld%lld",&a,&b);            a=TtoS[a];            int quan=b/seat;            a=a+quan*seat;            if(a<b)                a+=seat;            list[i]=point(a,b);        }        sort(list,list+ball,cmp);//排序,使得答案可以递推        ll ans=(1LL<<62);        ll last=0;//存储上一次的答案        for(ll i=0;i<ball;i++)            bb[i]=list[i].y-list[i].x;//预处理出B        //先算一遍算出last        for(ll j=0;j<ball;j++){            last+=list[j].x+bb[0]-list[j].y;        }        ans=last;        last+=seat;        for(ll i=1;i<ball;i++){            ll tans=0;            tans=last+ball*(bb[i]-bb[i-1]);//递推            ans=min(ans,tans);            last=tans+seat;        }        cout<<ans<<endl;    }    return 0;}/*42 3 31 21 12 11 42 3 51 21 12 11 21 31 43 7 53 5 71 52 13 31 52 52 100 21 511 5002 100010 55 17 310 55 5*/




原创粉丝点击