poj-3190-Stall Reservations

呂振麒 bio photo By 呂振麒 Comment

傳送門

Stall Reservations

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdio>
#include <vector>
#include <utility>
#include <functional>
#include <queue>
#include <cmath>
#include <algorithm>
#define MP make_pair
#define PB push_back
#define X first
#define Y second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;

int N, a, b, ans[50005], cnt;
vector<PIII> vec;
priority_queue<PII, vector<PII>, greater<PII> > pq;
int main(){
  scanf("%d", &N);
  for(int i=0;i<N;i++){
    scanf("%d %d", &a, &b);
    vec.PB(MP(MP(a,b), i));
  }
  sort(vec.begin(), vec.end(), less<PIII>() );
  pq.push(MP(0,1));
  cnt = 1;
  for(int i=0;i<vec.size();i++){
    if(pq.top().X < vec[i].X.X){
      ans[vec[i].Y] = pq.top().Y;
      pq.pop();
    }
    else
      ans[vec[i].Y] = ++cnt;
    pq.push(MP(vec[i].X.Y, ans[vec[i].Y]));
  }
  printf("%d\n", cnt);
  for(int i=0;i<N;i++)printf("%d\n", ans[i]);
}
comments powered by Disqus