K - Knapsack Collection Gym - 101482K(模拟)

Gerald’s job is to welcome the teams for this year’s NWERC at the airport in Linköping. One of his duties is to stand at the luggage carousel and collect all the knapsacks that the teams are bringing. Gerald is a lazy person, so he just stands at the same position of the carousel and waits for bags to pass by so he can pick them up.
The baggage carousel consists of s luggage slots,
numbered in ascending order from 0 to s − 1. Since
the baggage carousel is cyclic, luggage slots s − 1 and
0 also lie side by side. The carousel turns in such a
way that if Gerald stands in front of slot i at some point in time, he will stand in front of slot
(i + 1) mod s one time unit later.
In the beginning Gerald prepares a huge baggage cart at some position and stands there to
wait for luggage. When a knapsack arrives in front of Gerald, he needs t time units to take it and put it on the baggage cart. After these t time units he is ready to pick up another knapsack. As long as there are remaining knapsacks on the luggage carousel, Gerald always takes the next one to arrive at his position as soon as he is ready after putting away the previous one.
Now Gerald wonders about the effect of his choice of position on the time it will take him to finish this task. It is up to you to help Gerald calculate the minimum, maximum, and average time to pick up all knapsacks, taken over all s possible slots, which can appear in front of Gerald after preparation. Time starts when he has prepared the baggage cart at some slot of the baggage carousel and ends after he has put the last knapsack on the cart.
Input
The input consists of:
• onelinewiththreeintegersn(1≤n≤2000),s(1≤s≤107)andt(1≤t≤107), where n is the number of knapsacks to pick up, s is the number of slots of the carousel, and t is the number of time units Gerald needs to pick up a knapsack from the carousel and put it on the cart;
• onelinewithnintegersk1,…,kn (0 ≤ ki ≤ s−1for1 ≤ i ≤ n),theslotsofthe knapsacks.
There may be several knapsacks stacked on top of each other in the same slot, but Gerald can still only pick up one knapsack at a time.
Output
Output three lines of output containing the minimum, maximum, and average time to pick up all the luggage, over all s positions. The average time should be output as a reduced fraction in the form p/q.
NWERC 2014 Problem K: Knapsack Collection 21
Picture by Bernhard J. Scheuvens via Wikimedia Commons.
Sample Input 1
Sample Output 1
7 10 10000000 0000001
70000001
70000009
350000027/5
Sample Input 2
Sample Input 3
Sample Output 2
Sample Output 3
10 10 3 0022446688
39 40 79/2
9 10000000 1 072345618
9
10000000
12500021249991/2500000

题意:
人坐在同一个节点,装一个物品时间为s。
机场取行李的地方是环形的,每秒走一个格子。
每个物品有对应出现的位置。
人坐在哪个位置取完物时间最大、时间最小、时间平均值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <set>

using namespace std;

typedef long long ll;

const int maxn = 2005;
ll a[maxn];
multiset<ll>st;
ll n,s,t;
map<ll,ll>mp;

ll gcd(ll n,ll m) {
    return m == 0 ? n : gcd(m,n % m);
}

ll cal(ll tim) {
    multiset<ll>nowst;
    nowst = st;
    ll cost = 0;
    for(int j = 1;j <= n;j++) {
        auto it = nowst.lower_bound(tim);
        ll num;
        if(it == nowst.end()) {
            num = *nowst.begin();
            it = nowst.begin();
        }
        else {
            num = *it;
        }
        if(num >= tim) {
            cost += num - tim;
        }
        else {
            cost += (num - tim + s) % s;
        }
        cost += t;
        tim = num;
        tim = (tim + t) % s;
        nowst.erase(it);
    }
    return cost;
}

void solve() {
    ll ans = 0;
    ll mi = 1e18,mx = 0;
    ll cnta1 = 0;
    for(int i = 1;i <= n;i++) {
        ll tmp = cal(a[i]);
        if(i == 1) {
            cnta1 = tmp;
        }
        if(a[i] == a[i - 1]) continue;
        mi = min(tmp,mi);
        mx = max(tmp,mx);
        ans += tmp;
        if(a[i] - a[i - 1] >= 2 && i != 1) {
            ll num = a[i] - a[i - 1] - 1;
            ans += tmp * num + num * (num + 1) / 2;
        }
    }
    
    ll num = s - a[n] + a[1] - 1;
    ans += cnta1 * num + num * (num + 1) / 2;
    for(int i = 1;i <= n;i++) {
        if(mp[(a[i] + 1) % s] == 1) continue;
        ll tmp = cal((a[i] + 1) % s);
        mx = max(mx,tmp);
    }
    ll tmp = gcd(s,ans);
    s /= tmp; ans /= tmp;
    printf("%lld %lld %lld/%lld\n",mi,mx,ans,s);
}

int main() {
    a[0] = -1;
    scanf("%lld%lld%lld",&n,&s,&t);
    for(int i = 1;i <= n;i++) {
        scanf("%lld",&a[i]);
        st.insert(a[i]);
        mp[a[i]] = 1;
    }
    sort(a + 1,a + 1 + n);
    ll ans = 0;

    solve();
}

//7 10 10000000
//0 0 0 0 0 0 1

//7 100 10000000
//1 2 3 4 5 6 7

//7 9 10000000
//1 2 3 4 5 5 7
原创文章 930 获赞 38 访问量 5万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/105625476