Codeforces Round #542(Div. 2) D1.Toy Train

本文解析了Codeforces比赛中的D1糖果运输问题,介绍了一种利用vector记录每个位置运送糖果时间消耗的方法,通过排序和计算找到从每个位置出发的最小时间,最终输出每个起点的最长时间。

链接:https://codeforces.com/contest/1130/problem/D1

题意:

给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果。

求从每个位置开车的最小的时间。

思路:

vector记录每个位置运送完拥有糖果的时间消耗,为糖果数-1 * n 加上消耗最少时间的糖果。

对每个起点进行运算,取所有点中的最大值。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN = 200 + 10;

vector<int> station[MAXN];
int train[MAXN];

int main()
{
    int n, m;
    int s, c;
    scanf("%d%d", &n, &m);
    for (int i = 1;i <= m;i++)
    {
        scanf("%d%d", &s, &c);
        int cost;
        if (s <= c)
            cost = c - s;
        else
            cost = n - (s - c);
        station[s].push_back(cost);
    }
    for (int i = 1;i <= n;i++)
        sort(station[i].begin(), station[i].end());
    for (int i = 1;i <= n;i++)
    {
        int res = -1;
        for (int j = 1;j <= n;j++)
        {
            if (!station[j].size())
                continue;
            int cost;
            if (i <= j)
                cost = j - i;
            else
                cost = n - (i - j);
            int k = station[j].size() - 1;
            cost += k * n;
            cost += station[j][0];
            res = max(res, cost);
        }
        printf("%d ", res);
    }


    return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10447814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值