μ΄ μ νμ λ¬Έμ μμ μ£Όμ΄μ§ 쑰건μ λ°λΌ νμ΄ λ°©λ²μ΄ λ§€μ° λ€μνκ² λλ μ μλ€.
// Greedy κΈ°μ΄// μμ μ μ§ννλλ° κ±Έλ¦¬λ μκ° Tκ° μ£Όμ΄μ§μ§ μμλ€.// κ·Έ μμ μ μ±νν μ§ μν μ§λ§ 보면 λλ€.// 1. μ λ ¬ Phase// λλλ μκ° κΈ°μ€μΌλ‘ μ λ ¬νλ€.// 2. ν λΉ Phase// μμμλΆν° μ°¨λ‘λλ‘ ν λΉνλ€.struct Meeting {int s, e;};void solve(vector<Meeting>& v) {sort(v.begin(), v.end(), [&](Meeting& a, Meeting& b) -> bool {if (a.e != b.e) return a.e < b.e;return a.s < b.s;});int last = 0;int ans = 0;for(auto& item: v) {if (item.s >= last) ++ans, last = item.e;}printf("%d\n", ans);}
struct Job {int t;int d; // deadlinebool operator<(const Job& t) const {if (d != t.d) return d > t.d;return this->t < t.t;}};void solve(vector<Job>& v) {sort(v.begin(), v.end());int ctime = v[0].d - v[0].t;for(int i = 1; i < v.size(); ++i) {ctime = min(ctime, v[i].d); // backward schedulingctime -= v[i].t;}if (ctime < 0) printf("-1\n");else printf("%d\n", ctime);}
// κΈ°λ³Έμ μΌλ‘ μκ°μ 1λΆν° λκΉμ§ νμΌλ©΄μ ν λΉνλ κ²// λ€λ§, λ μ΄μ νλ³΄κ΅°μ΄ μμ λλ κΈ°λ‘μ΄ μλ μ§μ λΆν° λ€μ μμ// -> μ΄λ κ² μνλ©΄ κ΅¬κ° ν΄ κ²½μ° TLE// κ³Όμ μ λ€μ μμ½νλ©΄ μλμ κ°λ€. (timeμ λν sliding window κΈ°λ²)// 1. κ°μ₯ r_iκ° μμ μκ° μμ λΆν° μμ// 1-1. r_iμ κ°μ μκ°μ μμνλ μμ λ€μ λͺ¨λ pqμ λ£λλ€.// 2. κ·Έ μ€ d_iκ° κ°μ₯ μ§§μ λ μλΆν° μκ°μ 1μ© μ¦κ°μμΌκ°λ©΄μ μ€μ λ‘ ν λΉνλ€.// 2-1. ν λΉν λ νλ²μ λ€ νλκ² μλλΌ, νμ¬ μκ°μ 1κ°λ§ ν λΉνκ³ , λ€μ μκ°μ μ°μν΄μ 보면μ ν λΉνλ€.// 3. λ§μ½ λ³΄λ €λ λ μμ΄ μ΄λ―Έ λ³΄κ³ μλ μκ°λλ₯Ό μ§λκ°λ€λ©΄ λ²λ¦°λ€.void solve(map<ll, priority_queue<ll, vector<ll>, greater<>>>& M) {// M[a] = {1, 2, 3} - a μκ°μ μμνλ μΌμ΄ 1, 2, 3μ λλλ 3κ°μ§κ° μλ€.int ans = 0;// unit time: 1μll ctime = 0;priority_queue<ll, vector<ll>, greater<>> gpq; // global pqauto it = M.begin();for(ll ctime = 1; ; ++ctime) { // μκ°μ μ¦κ°μμΌκ°λ©΄μ λ³Έλ€.if (gpq.size() == 0) {// μ무κ²λ μμΌλ λ΄λ§λλ‘ μκ°μ λ§μΆλ€~if (it == M.end()) break;auto& cur = *it;ctime = cur.first;++it;}if (M.find(ctime) != M.end()) {it = M.find(ctime);auto& cur = *it;auto& pq = cur.second;while(pq.size()) gpq.push(pq.top()), pq.pop();++it;}// μ ν¨νμ§ μμ gpq κ°μ κΊΌλΈλ€.while (gpq.size() && gpq.top() < ctime) gpq.pop();// pqμμ κ°μ₯ μμκ±° νλ κΊΌλ΄μ μ΄λ€.if (gpq.size()) {gpq.pop();++ans;}}printf("%d\n", ans);}
void solve(vector<int>& chicken, vector<pair<int,int>>& cow, int N, int M) {sort(cow.begin(), cow.end());sort(chicken.begin(), chicken.end());// deadlineμ μμλλ‘ λ£μ. νμ¬ μ ν¨ν λ μλ€λ§ λ€μ΄κ° μκ² λλ€.priority_queue<int, vector<int>, greater<>> pq;int cowPtr = 0;int ans = 0;for(auto& c: chicken) {int cur = c; // νμ¬ λ³΄λ chickenμ T_ifor(; cowPtr < M; ++cowPtr) {if (cow[cowPtr].y < cur) continue; // λ²λ¦°λ€. μ΄λ―Έ λ€ μ§λκ°λ€.if (cow[cowPtr].x <= cur && cur <= cow[cowPtr].y) {pq.push(cow[cowPtr].y); // deadline μμλ‘ μ λ ¬νλ pqμ λ£κΈ°.} else {// μμΌλ‘ μΈ λ μλ€μ λ§λ¬λ€. λμ΄μ pqμ λ£μ§μκ³ μ’ λ£break;}}while(pq.size() && pq.top() < cur) pq.pop(); // μ ν¨νμ§ μμκ² λ²λ¦¬κΈ°if (pq.size()) {// printf("[d] deadline: %d / chicken: %d matched!\n", pq.top(), cur);pq.pop();++ans;}}printf("%d\n", ans);}
struct Range {int l, r;bool operator<(const Range& t) {if (l != t.l) return l < t.l;return r < t.r;}};void solve(vector<Range>& v) {sort(v.begin(), v.end()); // 빨리 μμνλ μμλλ‘ μ λ ¬priority_queue<int, vector<int>, greater<>> pq;for(auto& item: v) {if (pq.size() == 0) {pq.push(item.r);continue;}if (item.l >= pq.top()) { // μΌλ¨ λ€μ΄κ° μ μλ€.pq.pop();pq.push(item.r); // κΈ°μ‘΄ λ μμ λ체} else { // λͺ»λ€μ΄κ°λ caseμ΄λ€. (νμ¬ κ°μ₯ 빨리 λλλ λ μ보λ€λ μμμ΄ λΉ λ¦)pq.push(item.r);}}printf("%ld\n", pq.size());}