1. 程式人生 > >USACO 2009 FEB Fair Shuttle 廟會班車 貪心

USACO 2009 FEB Fair Shuttle 廟會班車 貪心

cap ppr 告訴 strong route walk 大於 OS arr

題目

題目描述

Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his cows are not in such good shape; a full day of walking around the fair leaves them exhausted. To help them enjoy the fair, FJ has arranged for a shuttle truck to take the cows from place to place in the fairgrounds.

FJ couldn‘t afford a really great shuttle, so the shuttle he rented traverses its route only once (!) and makes N (1 <= N <= 20,000) stops (conveniently numbered 1..N) along its path. A total of K (1 <= K <= 50,000) groups of cows conveniently numbered 1..K wish to use the shuttle, each of the M_i (1 <= M_i <= N) cows in group i wanting to ride from one stop S_i (1 <= S_i < E_i) to another stop E_i (S_i < E_i <= N) farther along the route.

The shuttle might not be able to pick up an entire group of cows (since it has limited capacity) but can pick up partial groups as appropriate.

Given the capacity C (1 <= C <= 100) of the shuttle truck and the descriptions of the groups of cows that want to visit various sites at the fair, determine the maximum number of cows that can ride the shuttle during the fair.

逛逛集市,兌兌獎品,看看節目對農夫約翰來說不算什麽,可是他的奶牛們非常缺乏鍛煉——如果要逛完一整天的集市,他們一定會筋疲力盡的。所以為了讓奶牛們也能愉快地逛集市,約翰準備讓奶牛們在集市上以車代步。但是,約翰木有錢,他租來的班車只能在集市上沿直線跑一次,而且只能停靠N(1 ≤N≤20000)個地點(所有地點都以1到N之間的一個數字來表示)。現在奶牛們分成K(1≤K≤50000)個小組,第i 組有Mi(1 ≤Mi≤N)頭奶牛,他們希望從Si跑到Ti(1 ≤Si<Ti≤N)。

由於班車容量有限,可能載不下所有想乘車的奶牛們,此時也允許小裏的一部分奶牛分開乘坐班車。約翰經過調查得知班車的容量是C(1≤C≤100),請你幫助約翰計劃一個盡可能滿足更多奶牛願望的方案。

輸入輸出格式

輸入格式:

【輸入】

第一行:包括三個整數:K,N和C,彼此用空格隔開。

第二行到K+1行:在第i+1行,將會告訴你第i組奶牛的信息:Si,Ei和Mi,彼

此用空格隔開。

輸出格式:

【輸出】

第一行:可以坐班車的奶牛的最大頭數。

輸入輸出樣例

輸入樣例#1: 復制
8 15 3
1 5 2
13 14 1
5 8 3
8 14 2
14 15 1
9 12 1
12 15 2
4 6 1
輸出樣例#1: 復制
10

說明

【樣例說明】

班車可以把2頭奶牛從1送到5,3頭奶牛從5送到8,2頭奶牛從8送到14,1頭

奶牛從9送到12,1頭奶牛從13送到14,1頭奶牛從14送到15。

分析

這題用了一個非常有趣的貪心。首先要做的是所有小組按照他們的終點站排序,然後在這個小組所要經過的區間裏找,如果之前經過過的小組已經使得在這條路上的某一個站點的剩余座位是為0,就說明這個小組沒有人能上車。如果大於0,那麽就可以上這麽多人。然後在這個區間裏減掉這個最少人數。

註意,如果不加一點優化會TLE。我們的思路導致主要的時間消耗是在掃每個隊的經過區間來計算最小剩余座位。那麽很容易想到如果已經掃到了0,直接退出循環。另外,如果為0,就不必在減去了,這樣可以剩下大把時間。

程序

#include <bits/stdc++.h>
using namespace std;
const int MAXK = 50000 + 1;
const int MAXN = 20000 + 1;
struct group
{
    int s, e, m;
}g[MAXK];
int k, n, c, st[MAXN], Min_Seats, ans;
bool comp(group x, group y)
{
    return x.e < y.e;
}
int main()
{
    cin >> k >> n >> c;
    for (int i = 1; i <= k; i++)
        cin >> g[i].s >> g[i].e >> g[i].m;
    for (int i = 1; i <= n; i++)
        st[i] = c;
    sort(g+1, g+(k+1), comp);
    for (int i = 1; i <= k; i++)
    {
        Min_Seats = g[i].m;
        for (int j = g[i].s; j < g[i].e; j++)
        {
            Min_Seats = min(Min_Seats, st[j]);
            if (!Min_Seats)
                break;
        }
        if (Min_Seats)
        {
            ans += Min_Seats;
            for (int j = g[i].s; j < g[i].e; j++)
                st[j] -= Min_Seats;
        }
    }
    cout << ans << endl;
    return 0;
}

USACO 2009 FEB Fair Shuttle 廟會班車 貪心