ccpc 2017秦皇岛站 A-balloon robot

本文介绍了一个关于竞赛编程中机器人送气球的问题,旨在寻找使所有团队不满情绪最小化的机器人起始位置。通过预测不同团队解决问题的时间及座位安排,利用算法确定最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be n teams participating in the contest, and the contest will be held on a huge round table with m seats numbered from 1 to m in clockwise order around it. The i-th team will be seated on the si-th seat. 

BaoBao, an enthusiast for competitive programming, has made p predictions of the contest result before the contest. Each prediction is in the form of (ai,bi), which means the ai-th team solves a problem during the bi-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 ta-th time unit, and the balloon is sent to them during the tb-th time unit, then the unhappiness of the team will increase by tb-ta. 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 k-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 i-th(1 ≤ i < m) seat, it will move to the (i + 1)-th seat; If the robot is currently on  the  m-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 k of the robot and calculate the minimum total unhappiness of all the teams according to BaoBao’s predictions. 

输入描述:

There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first line contains three integers n,m and p(1< n ≤ 105), (n ≤ m ≤ 109), (1 ≤ p ≤ 105),indicating the number of participating teams, the number of seats and the number of predictions.
The second line contains n integers (s1,s2,…, sn) (1 ≤ s≤ m, and s≠ sj for all i ≠ j),indicating the seat number of each team.
The following p lines each contains two integers aand b(1 ≤ a≤ n,1 ≤ b≤ 109), indicating that the ai-th team solves a problem at time baccording to BaoBao’s predictions.
It is guaranteed that neither the sum of n nor the sum of p over all test cases will exceed 5×105.

输出描述:

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

输入

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

输出

1
4
5
50

说明

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.
看起来好想挺难的。。。只要把所有的不开心值都转化为从0开始的,然后将不开心值排序,枚举每种不开心的位置,则当前之前的所有点都要加(m - cur_pos)的不开心值, 之后的点都减去(cur_pos) 求出最小的值就好了
#include <unordered_map>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int maxn = 100005;
long long num[maxn];
int pos[maxn];
int main() {
        //freopen("in.txt", "r", stdin);
    int t, n, m, p;
    cin >> t;
    while (t--) {
        cin >> n >> m >> p;
        for (int i = 1; i <= n; ++i) {
            cin >> pos[i];
        }
        long long sum = 0;
        for (int i = 1; i <= p; ++i) {
            int a, b;
            cin >> a >> b;
            num[i] = (pos[a] - b % m + m) % m;//从0开始的不开心值
            sum += num[i];
        }
        sort(num + 1, num + 1 + p);
        long long res = 1e16;
        for (int i = 1; i <= p; ++i) {
            res = min(res, sum + static_cast<long long>((i - 1)) * (m - num[i]) - static_cast<long long>(((p - i + 1)) * num[i]));
        }
        cout << res << endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值