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 .

<h4< dd="">Output

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

<h4< dd="">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

<h4< dd="">Sample Output

1
4
5
50

<h4< dd="">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.

题意:就是CCPC比赛,有n支队伍,m个座位,n支队伍坐在这些座位上面,一个机器人从任意位置开始每次移动一个位置分发气球,如果到了m,则下一次从新回到第一个位置;每只队伍有一个

愤怒值为过题时间和分发气球的时间差,让你求这些队伍愤怒值和的最小值;

题解:窝萌阔以假设它从1号店开始,算出每次请求的t-b的值并保存在数组h中,值的范围在(0~m-1)之间。若起点向后移动一个则数组h中的数据都加一,且等于M的都变为0。由于M有10的9次方所以不能遍历所有的可能。

所以将h数组排序,每次将最大值变为0,即整个数组都加上(m-最大值)

参考代码:

 #include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
const int maxn=1e5+;
int t;
ll n,m,p,x,y,a[maxn],h[maxn];
int main()
{
cin>>t;
while(t--)
{
ll ans=;
cin>>n>>m>>p;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<p;i++)
{
cin>>x>>y;
h[i]=(a[x]-(y%m)+m)%m;
ans+=h[i];
}
ll sum=ans,cp=;
sort(h,h+p);
for(int i=p-;i>=;i--)
{
int temp=;
while(h[i-temp]==h[i]&&i>=temp) temp++;
h[i]+=cp; cp+=m-h[i];
ans+=(p-temp)*(m-h[i]); ans-=temp*h[i];
sum=min(sum,ans);
i-=temp-;
}
cout<<sum<<endl;
}
return ;
}

  

2017 CCPC秦皇岛 A题 A Ballon Robot的更多相关文章

  1. 2017 CCPC秦皇岛 M题 Safest Buildings

    PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parach ...

  2. 2017 CCPC秦皇岛 L题 One Dimensions Dave

    BaoBao is trapped in a one-dimensional maze consisting of  grids arranged in a row! The grids are nu ...

  3. 2017 CCPC秦皇岛 E题 String of CCPC

    BaoBao has just found a string  of length  consisting of 'C' and 'P' in his pocket. As a big fan of ...

  4. 2017 CCPC秦皇岛 H题 Prime set

    Given an array of  integers , we say a set  is a prime set of the given array, if  and  is prime. Ba ...

  5. 2017 CCPC秦皇岛 G题 Numbers

    DreamGrid has a nonnegative integer . He would like to divide  into nonnegative integers  and minimi ...

  6. 2017 ccpc哈尔滨 A题 Palindrome

    2017 ccpc哈尔滨 A题 Palindrome 题意: 给一个串\(T\),计算存在多少子串S满足\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\) 思路: 很明显这里的回文串长 ...

  7. HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)

    题目链接  2017 CCPC Hangzhou  Problem E 题意  给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...

  8. HDU 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)

    题目链接  2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块.    分块的时候满足每个块是一个 ...

  9. 2018 CCPC秦皇岛 C题 Crusader Quest

    Crusaders Quest is an interesting mobile game. A mysterious witch has brought great darkness to the ...

随机推荐

  1. 前端Leader你应该知道的NPM包管理机制

    npm install 命令 首先总结下npm 安装一个模块包的常用命令. /* 模块依赖会写入 dependencies 节点 */ npm install moduleName npm insta ...

  2. jquery正确获取iframe里元素的方法

    <iframe id="_ae_frame" width="100%" height="100%" frameborder=" ...

  3. 使用ASP.NET Core 3.x 构建 RESTful API - 3.1 资源命名

    之前讲了RESTful API的统一资源接口这个约束,里面提到了资源是通过URI来进行识别的,每个资源都有自己的URI.URI里还涉及到资源的名称,而针对资源的名称却没有一个标准来进行规范,但是业界还 ...

  4. java多线程与线程并发五:多个线程访问共享对象和数据的方式

    本节的内容主要是对前面几节提到的线程间共享数据的方式做一个总结. 线程之间共享数据有以下几种方式: 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象. 2.如果每个线程执行的代码不同 ...

  5. 关于GDAL读写Shp乱码的问题总结

    目录 1. 正文 1.1. shp文件本身的编码的问题 1.2. 设置读取的编码方式 1.2.1. GDAL设置 1.2.2. 解码方式 1.2.3. 其他 2. 参考 1. 正文 最近在使用GDAL ...

  6. c#控制台玩飞行棋游戏

    using System; namespace Game{ class Program { //用静态字段模拟全局变量 public static int[] Maps = new int[100]; ...

  7. 剑指Offer-20.包含min函数的栈(C++/Java)

    题目: 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 分析: 因为题目要求得到栈中最小元素的min函数时间复杂度为O(1),这里便不选择遍历栈 ...

  8. scrapy介绍及使用

    scrapy的流程 其流程可以描述如下: 调度器把requests-->引擎-->下载中间件--->下载器 下载器发送请求,获取响应---->下载中间件---->引擎-- ...

  9. ubuntukylin16.04LTS(乌班图麒麟版长期支持版,并非银河麒麟)安装体验

    最近,国产银河麒麟版在政府部门推广使用.我有幸接触了,感觉还是不错的.这次政府软件正版化整改中,也列入了windows和银河麒麟的选项.我想试安装一下,可是没找到.就近找了它的类似系统ubuntuky ...

  10. ubuntu 16.04源码编译OpenCV教程 | compile opencv on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/15f5c3e8/,欢迎阅读! compile opencv on ubuntu 16.04 Series Part 1: comp ...