P1607 [USACO09FEB] temple shuttle Fair Shuttle

P1607 [USACO09FEB] temple shuttle Fair Shuttle


Title: stroll market, against the awarding product to see what the program is not for Farmer John, but his cows are very lack of exercise - if you want to walk around all day in the market, they will be exhausted. To make the cows could happily visiting the market, ready to let John cows at the market in cars. However, John Wood the money, he rented bus along only once in the bazaar run a straight line, and can only be docked \ (N (1 ≤N≤20000) \ ) locations (all locations are at the 1 to N a number between expressed). Now the cows were divided into \ (K (1≤K≤50000) \) groups, the first group i \ (M_i (1 ≤M_i≤N) \ ) cows they want from the \ (S_i \) went \ ( t_i (≤S_i. 1 <T_i≤N) \) .

Due to limited bus capacity, may contain less than all the cows want to ride, this time also allows small part of the cow in separate bus ride. John after investigation that the shuttle capacity is \ (C (1≤C≤100) \) , John help you plan a possible solution to satisfy the desire for more cows.


answer:

For this question, it is clear that a greedy, and we must be considered as having to go back mechanism of greed. First, in each station, conduct some judgments:

  1. The cows in the car can get off and get off
  2. On this site all the car on the car cows
  3. If the number exceeds, the farthest destination cows get out
#include <iostream>
#include <set>
#include <algorithm>

using namespace std;
typedef long long ll;

const int maxn = 500005;

// 一组牛
struct group {
    ll s, t, m;
    group() {}
    group(ll s, ll t, ll m) : s(s), t(t), m(m) {}
    // set内部按照终点站顺序排序
    friend bool operator < (const group &a, const group &b) {
        return a.t < b.t;
    }
} cows[maxn];
ll k, n, c, sum_on_car, ans;

// 在车上的牛的组
multiset<group> cow_set;

// 按照起点站顺序排序
bool cmp(group a, group b) {
    if (a.s == b.s) {
        if (a.t == b.t) {return a.m < b.m;}
        else {return a.t < b.t;}
    } else {return a.s < b.s;}
}

int main() {
    // 读
    cin >> k >> n >> c;
    for (int i = 1; i <= k; i ++)
        cin >> cows[i].s >> cows[i].t >> cows[i].m;
    sort(cows + 1, cows + 1 + k, cmp);
    // 从第一站开始遍历上车
    for (int i = 1, j = 0; i <= n; i ++) {
        multiset<group>::iterator begin_iter = cow_set.begin();
        // 到站下车 (终点站顺序排序)
        while (begin_iter -> t == i) {
            sum_on_car -= begin_iter -> m;
            cow_set.erase(begin_iter);
            begin_iter = cow_set.begin();
        }
        // 全部上车
        for (int t = j + 1; t <= k && cows[t].s == i; t ++) {
            j ++;
            cow_set.insert(cows[t]);
            sum_on_car += cows[t].m;
            ans += cows[t].m;
        }
        // 人数超标,删最远的牛
        while (sum_on_car > c) {
            ll delta = sum_on_car - c;
            multiset<group>::iterator iter = cow_set.end();
            iter --;
            ll all_cow_farthest = iter -> m;
            cow_set.erase(iter);
            if (delta >= all_cow_farthest) {
                sum_on_car -= all_cow_farthest;
                ans -= all_cow_farthest;
            } else {
                ll cow_tmp_s = iter -> s, cow_tmp_t = iter -> t, cow_tmp_m = iter -> m;
                cow_set.insert(group(cow_tmp_s, cow_tmp_t, all_cow_farthest - delta));
                sum_on_car -= delta;
                ans -= delta;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/jeffersonqin/p/11210925.html