[Link] http://acm.hdu.edu.cn/contests/contest_show problem.php?Pid=1010&cid=767
[title]
Given some intervals, each machine runs in these intervals, but a machine can only run in one interval at most (multiple intervals in the same place require multiple machines), and the machine can not be turned off again, seeking the shortest running time in the case of using the least machine.
Running time is the sum of all machine running time.
[Abstract]
Because the priority is the smallest number of machines;
First, rank the intervals in ascending order according to the left endpoint.
Then enumerate the intervals in order.
For intervals encountered
Which machine will handle it?
Of course, it's the machine that ends the latest and is on the left side of the interval.
In this way, each additional time is the least;
And those that end early, try not to use them first.
Because with those words, more time will be added.
If there's no machine available, you'll have to use a new one.
Such a process ensures that the minimum number of machines are used.
Because it's used as much as possible.
Write a multiset and save the end time of each machine.
The new machine is supposed to start at the beginning of the interval.
Note that the machine can't be turned on when it's closed. So it has to be kept on, even if there's nothing in the middle.
Never use upper_bound(set.begin(),set.end(),a[i]).
To write like this
set.upper_bound(a[i]);
Otherwise it will overtime!!!
[Number of errors]
1
[introspection]
1.set.upper_bound(a[i]);
2. Attention should be paid to the topic.
[Code]
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <iomanip> #include <set> #include <cstdlib> #include <cmath> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second #define ms(x,y) memset(x,y,sizeof x) #define ri(x) scanf("%d",&x) #define rl(x) scanf("%lld",&x) #define rs(x) scanf("%s",x) #define oi(x) printf("%d",x) #define ol(x) printf("%lld",x) #define oc putchar(' ') #define os(x) printf(x) #define all(x) x.begin(),x.end() #define Open() freopen("F:\\rush.txt","r",stdin) #define Close() ios::sync_with_stdio(0) typedef pair<int, int> pii; typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 }; const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 }; const double pi = acos(-1.0); const int N = 1e5; multiset <int> myset; pii a[N + 10]; int n; LL ans = 0; int main() { //Open(); //Close(); int T; ri(T); while (T--) { ri(n); rep1(i, 1, n) ri(a[i].fi), ri(a[i].se); sort(a + 1, a + 1 + n); myset.clear(); LL ans = 0; rep1(i, 1, n) { auto temp = myset.upper_bound(a[i].fi); //cout <<"i="<<i<<' ';cout << (*temp) << endl; if (temp == myset.begin()) { myset.insert(a[i].se); ans = (ans + a[i].second - a[i].first); continue; } temp--; ans = (ans + a[i].second - *temp); myset.erase(temp); myset.insert(a[i].se); } oi((int)myset.size()); oc; ol(ans); puts(""); } return 0; }