Luogu P1607 [usaco 09feb] Temple Fair Shuttle, p1607usaco 09feb
Description
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 varous sites at the fair, determine the maximum number of cows that can ride the shuttle during the fair.
Visit the market, exchange prize products, and watch the program. It is nothing for Farmer John, but his cows are very hard-they will be exhausted if they want to finish the whole day of the market. So in order to make the cows have a pleasant visit to the market, John is going to let the cows go by car in the market. However, John Mu has money. The shuttle he rented can only run in a straight line on the market, and can only dock N (1 ≤ N ≤ 20000) location (all locations are represented by a number between 1 and N ). Now the cows are divided into K (1 ≤ K ≤ 50000) groups, and the I group contains Mi (1 ≤ Mi ≤ N) cows, they want to run from Si to Ti (1 ≤ Si <Ti ≤ N ).
Due to the limited capacity of the shuttle bus, it may not be able to carry all the cows who want to ride. At this time, some of the cows in the small can also take the shuttle bus separately. John found that the capacity of the shuttle bus was C (1 ≤ C ≤ 100). Please help John plan a solution to satisfy as many cows as possible.
Input/Output Format
Input Format:
[Input]
The first line contains three integers: K, N, and C, which are separated by spaces.
Row 2 to row K + 1: In row I + 1, it will tell you the information about the cows in Group I: Si, Ei and Mi.
This is separated by spaces.
Output Format:
[Output]
Row 1: Maximum number of cows that can take the shuttle bus.
Input and Output sample input sample #1:
8 15 31 5 213 14 15 8 38 14 214 15 19 12 112 15 24 6 1
Output sample #1:
10
Description
[Example]
The shuttle bus can send 2 cows from 1 to 5 to 8 to 14 to 1
From 9 to 12, from 13 to 14, from 14 to 15.
Zhw is talking about the line segment tree.
But I want to ask ,,
Why do we need to use a line segment tree for brute force questions ???????
Idea: sort by end point. For each line segment, query the minimum value in the covered range,
Then we can discuss the classification based on the weight of the line segment.
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <queue> using namespace std; const int MAXN = 100001; inline void read (int & n) {char c = getchar (); bool flag = 0; n = 0; while (c <'0' | c> '9 ') c = '-'? Flag = 1, c = getchar (): c = getchar (); while (c> = '0' & c <= '9 ') n = n * 10 + c-48, c = getchar (); flag = 1? N =-n: n = n;} struct node {int bg, ed, num;} cow [MAXN]; int comp (const node & a, const node & B) {if (. ed! = B. ed) return. ed <B. ed; else return. bg <B. bg;} int have [MAXN]; // In the I position, how many cows have been installed in the car int main () {int k, n, m; read (k ); read (n); read (m); for (int I = 1; I <= k; I ++) read (cow [I]. bg), read (cow [I]. ed), read (cow [I]. num); sort (cow + 1, cow + k + 1, comp); int ans = 0; for (int I = 1; I <= k; I ++) {if (have [cow [I]. bg]> = m) continue; int now = 0x7fffff; for (int j = cow [I]. bg; j <= cow [I]. ed; j ++) {now = min (now, m-have [j]); if (now = 0) break;} if (now! = 0) {if (now> = cow [I]. num) {for (int j = cow [I]. bg; j <cow [I]. ed; j ++) have [j] + = cow [I]. num; ans + = cow [I]. num;} else {for (int j = cow [I]. bg; j <cow [I]. ed; j ++) have [j] + = now; ans + = now ;}} printf ("% d", ans); return 0 ;}