|
| 1 | +#include<bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +#define INF 0x3f3f3f3f |
| 6 | +#define INF64 0x3f3f3f3f3f3f3f3f |
| 7 | +#define ms(x,v) memset(x,(v),sizeof(x)) |
| 8 | + |
| 9 | +typedef long long LL; |
| 10 | +typedef pair<int,int> PII; |
| 11 | +typedef tuple<int,int,int> TIII; |
| 12 | +typedef pair<PII,int> PIII; |
| 13 | +const int MAXN = 1e6+10; |
| 14 | + |
| 15 | +int n, q; |
| 16 | +vector<PII> book; |
| 17 | + |
| 18 | +bool ok(int k){ |
| 19 | + if(k==0)return true; |
| 20 | + vector<int> bg(q,0); |
| 21 | + |
| 22 | + for(int i=0 ; i< q ; ++i){ |
| 23 | + int sweep_to = max(book[i].first,bg[i]),ed = book[i].second; |
| 24 | + int next_start = ed; |
| 25 | + int cnt =0; |
| 26 | + |
| 27 | + for(int j=i+1 ; j < q ; ++j){ |
| 28 | + int l = book[j].first,r = book[j].second; |
| 29 | + if(l >ed)break; |
| 30 | + if(r <= ed){// must before the ith booking |
| 31 | + if(l >sweep_to)cnt += l - sweep_to; |
| 32 | + sweep_to = max(sweep_to,r); |
| 33 | + if(cnt >=k){// just ok |
| 34 | + next_start = l - (cnt -k); |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + assert(sweep_to <= ed); |
| 40 | + if(cnt <k){ |
| 41 | + cnt += ed - sweep_to; |
| 42 | + if(cnt <k)return false; |
| 43 | + next_start = ed - (cnt - k); |
| 44 | + } |
| 45 | + |
| 46 | + // update left start point |
| 47 | + |
| 48 | + for(int j = i+1; j<q ; ++j){ |
| 49 | + int l = book[j].first,r = book[j].second; |
| 50 | + if(l>=next_start)break;// can book before ith booking |
| 51 | + if(book[j].second > ed){// those booking must after ith booking |
| 52 | + bg[j] = max(bg[j],ed); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +int solve(){ |
| 61 | + sort(book.begin(), book.end()); |
| 62 | + int lo =0,hi = n; |
| 63 | + while(lo <= hi){ |
| 64 | + int mid = (hi + lo) >> 1; |
| 65 | + if(ok(mid))lo = mid +1; |
| 66 | + else hi = mid -1; |
| 67 | + } |
| 68 | + assert(lo-1>=0); |
| 69 | + return lo -1; |
| 70 | +} |
| 71 | + |
| 72 | +int main(){ |
| 73 | + |
| 74 | + ios :: sync_with_stdio(0); |
| 75 | + cin.tie(0); |
| 76 | + std::cout.precision(8); |
| 77 | + std::cout.setf( std::ios::fixed, std:: ios::floatfield ); |
| 78 | + |
| 79 | + int T; |
| 80 | + cin >> T; |
| 81 | + // cout << "T = " << T << "\n"; |
| 82 | + for(int t =1 ; t <= T; ++t){ |
| 83 | + |
| 84 | + cin >> n >> q; |
| 85 | + book.resize(q); |
| 86 | + for(int i=0 ; i<q ; ++i) |
| 87 | + { |
| 88 | + int l,r; |
| 89 | + cin >> l >> r; |
| 90 | + book[i] = {--l,r}; |
| 91 | + } |
| 92 | + |
| 93 | + auto ans = solve(); |
| 94 | + std::cout << "Case #"<<t<<": "<<ans<<"\n"; |
| 95 | + } |
| 96 | + return 0; |
| 97 | +} |
0 commit comments