Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
建议按照开始时间升序排序,若以结束时间升序排序比较麻烦容易出错经历了TLE->WA,这题我记住了 1)不要用set 2)建议按照开始时间升序排序,维护一个结束时间的最小堆 代法附下: #include <iostream> #include <queue> #include <utility> #include <algorithm> #include <cstdio> using namespace std; typedef pair<int, int> P; struct Itv{ int s, g; int order; bool operator < (const Itv& b) const { if(this->s != b.s) return this->s < b.s; else return this->g < b.g; } }; const int maxn = 50000 + 5; int N; priority_queue<P, vector<P>, greater<P> > pq; Itv itvs[maxn]; int stall[maxn]; int tanxin() { pq.push(make_pair(itvs[0].g, 1)); stall[itvs[0].order] = 1; for(int i = 1; i<N; i++) { P p = pq.top(); if(itvs[i].s > p.first) { stall[itvs[i].order] = p.second; pq.pop(); pq.push(make_pair(itvs[i].g, p.second)); } else { stall[itvs[i].order] = pq.size() + 1; pq.push(make_pair(itvs[i].g, pq.size() + 1)); } } return pq.size(); } int main() { scanf("%d", &N); for(int i = 0; i<N; i++) { scanf("%d %d", &itvs[i].s, &itvs[i].g); itvs[i].order = i; } sort(itvs, itvs + N); int minnum = tanxin(); printf("%d\n", minnum); for(int i = 0; i<N; i++) printf("%d\n", stall[i]); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator