Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## 建议按照开始时间升序排序，若以结束时间升序排序比较麻烦容易出错

Posted by 2510243467 at 2021-03-26 19:17:17 on Problem 3190
```经历了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: