zoj 3981 Balloon Robot - - 2017秦皇島A題(思維題)

鏈接

Balloon Robot
Time Limit: 1000 msMemory 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:

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


  • 題目大意:就是一個機器人發氣球(類似於比賽A題發氣球),順時針圍着m個座位組成的圓轉,其中問機器人從哪個位置開始,使不開心值的總和最小,只要輸出不開心值總和的最小值。

分析:

假設有4個位置,每個位置上有A題的時間t1,t2,t3,t_1,t_2,t_3,\cdots,
在這裏插入圖片描述

先假設機器人從位置1出發,那麼對於某組A題的時間b,座位號爲a,求其不開心值,
b可以表示成b=km+x,(其中x>=0)
那麼機器人在其A題後到達的時間爲:
若k
m+a-1>=b,到達時間爲k*m+a-1
否則爲 (k+1)*m+a-1
a-1表示從位置1到位置a的時間,k表示第幾輪
那麼不開心值(一定爲正)爲:td= (a-1-x+m)%m =( a-1-b%m+m)%m
然後q個不開心值排序,其總和爲sum,枚舉i=0…q-1
當枚舉到第i個時,表示該不開心值對應的位置作爲起始,就是從第一個位置順時針向右移動了td[i],那麼q個數都向右移了td[i],即在sum基礎上-q*td[i],7但i前面的會爲負數,故加上i*m

代碼:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long ll;
const int N=1e5+10;



int pos[N];
int td[N];

int main()
{
	int t,n,a,id;
	ll m,q,b;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%lld%lld",&n,&m,&q);
		for(int i=1;i<=n;i++)
			scanf("%d",&pos[i]);
		ll sum=0;
		for(int i=0;i<q;i++)
		{
			scanf("%d%lld",&a,&b);
			td[i]=(pos[a]-1-b%m+m)%m;
			sum +=td[i];
		}
		sort(td,td+q);
		ll ans=(ll)1e18;
		for(int i=0;i<q;i++)
		{
			ans = min(ans, sum + i*m - td[i]*q);
		}
		printf("%lld\n",ans);
	}
	return 0;
}


參考:
  • https://www.cnblogs.com/zyb993963526/p/7763983.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章