P2869 [USACO07DEC] food herbivores Gourmet Grazers

P2869 [USACO07DEC] food herbivores Gourmet Grazers


Title: John cows for food more and more picky. Now, the store has copies M pasture available for sale, large dairy cattle eat, each cow pasture only for consumption. The price of the i-th forage for Pi, the taste is Qi. John, a total of N cows, he should order a copy of forage per cow, i-cow pasture requirements of its price not less than Ai, the taste of not less than Bi. I ask John how to choose grass for each cow, to let him spend the least money?


Solution: cows price demands and grass price requirements sorted, the sorted can be obtained as follows:
\ [\ the begin {align = left} (A_1, B_1), (A_2, B_2), \ cdots, (A_N, B_N), ~ a_i ≤ A_j, ~ 1 ≤ i <j ≤ N \\ (P_1, Q_1), (P_2, Q_2), \ cdots, (P_M, Q_M), ~ P_i ≤ P_j, ~ 1 ≤ i <j ≤ M \ end {align} \]
from 0 calendar starting with the first pass, if \ ((P_1, Q_1) \ ) may cause the \ (A_x <P_1 <A_. 1} + {X \) , then the \ ((B_1, 1) , \ cdots, (B_x, x ) \) into \ (S \) , because it can be demand for quality sorting in accordance cow, that each number given cow pasture be \ (T \) , wherein \ (Q_t ≤ Q_1 <Q_. 1 {T} + \) , i.e. \ (T = s.lower \ _bound (Q_1) -. 1 \) , but we will find that there is a problem, because \ (lower \ _bound \) if looking less, will be positioned ratio (\ Q_1) \ on the large number , so here you can use the techniques: the \ (S \) in descending order (the original is in ascending order) , justEach number will be multiplied by \ (--1 \) can , when taken out also take \ (--1 \) . For subsequent consideration of forage, can be found in \ (≥ P_i P_1 \) (already sorted up), so the \ (S \) cows only need to care about quality, we do not care about the price.

In summary we can see is the need to maintain a storage cow \ (multiset \) , which can be used in determining whether there is a grass tips: Direct use of iterators \ (std :: multiset \ text { <} int \ text {>} :: = Cow ITER Iterator ~ ~ \ _set.lower \ _bound (Q_ {I}) \) . For this iterator, since the collection was placed in descending order, we only need to operate the iterator can determine it possible to use a straw. Now we assume that: a grass quality is lower than the cows now set the minimum required quality, it is clear that this time returned \ (iter \) is not among the collection (because the default look for a bigger), then there must be \ (ITER Cow == \ _set.end () \) (according to (End () \) \ defined).

Code:

#include <iostream>
#include <algorithm>
#include <set>

using namespace std;
typedef long long ll;

const int maxn = 100005;

ll n, m;
pair<ll, ll> cow[maxn], grass[maxn];
multiset<ll> cow_set;

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i ++)
        cin >> cow[i].first >> cow[i].second;
    for (int i = 1; i <= m; i ++)
        cin >> grass[i].first >> grass[i].second;
    sort(cow + 1, cow + 1 + n);
    sort(grass + 1, grass + 1 + m);

    ll ans = 0;
    for (int grass_i = 1, cow_i = 0; grass_i <= m; grass_i ++) {
        ll now_grass_cost = grass[grass_i].first;
        ll now_grass_quality = grass[grass_i].second;
        for (int i = cow_i + 1; i <= n && cow[i].first <= now_grass_cost; i ++) {
            // 符号:将序排列
            cow_set.insert(-cow[i].second);
            cow_i ++;
        }
        set<ll>::iterator iter = cow_set.lower_bound(-now_grass_quality);
                // 草品质达到
        if (iter != cow_set.end()) {
            cow_set.erase(iter);
            ans += now_grass_cost;
        }
    }
    // 如果还有牛剩下来 -> 这些牛的要求没有被满足 -> 答案为-1 
    if (!cow_set.empty()) ans = -1;
    cout << ans << endl;
}

Guess you like

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