#include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> #include <bitset> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second typedef bool(*Comparator)(int, int); list<int> reservations[1000000]; int a[1000000], b[1000000], przyrzad[1000000]; int last_possible[1000000]; int schedule[1000000]; const int INF = 2000000000; bool compare_last(int i, int j) { if (a[i] != a[j]) return a[i] < a[j]; else if (b[i] != b[j]) return b[i] < b[j]; else return i < j; } bool compare_first(int i, int j) { return last_possible[i] > last_possible[j]; } struct CompareFirst { bool operator()(const int i, const int j) { return compare_first(i, j); } }; priority_queue<int, vector<int>, CompareFirst> queued[1000000]; int main() { int N, K; scanf("%d%d", &N, &K); map<int, int> mapping; REP(i,N) { int c; scanf("%d%d%d", &a[i], &b[i], &c); if (mapping.find(c) == mapping.end()) { int S = mapping.size(); mapping[c] = S; } przyrzad[i] = mapping[c]; reservations[przyrzad[i]].pb(i); } K = mapping.size(); REP(k,K) { vector<PII> end_and_num; FOREACH(it, reservations[k]) end_and_num.pb(mp(b[*it], *it)); sort(end_and_num.rbegin(), end_and_num.rend()); int o = 0; int ti = end_and_num[0].st; priority_queue<int, vector<int>, Comparator> Q(compare_last); while (o < end_and_num.size() || Q.size() > 0) { if (o < end_and_num.size() && end_and_num[o].st >= ti) { Q.push(end_and_num[o].nd); o++; continue; } else if (!Q.empty()) { int v = Q.top(); Q.pop(); if (ti < a[v]) { printf("NIE\n"); return 0; } last_possible[v] = ti; --ti; } else { ti = end_and_num[o].st; } } } vector<PII> start_and_num; REP(i,N) start_and_num.pb(mp(a[i], i)); int next_to_queue = 0; sort(start_and_num.begin(), start_and_num.end()); vector<int> batch; int deadline = INF; int ti = 0; while (next_to_queue < N || !batch.empty()) { if (next_to_queue < N && start_and_num[next_to_queue].st <= ti) { int v = start_and_num[next_to_queue].nd; queued[przyrzad[v]].push(v); deadline = min(deadline, last_possible[v]); if (queued[przyrzad[v]].size() == 1) batch.pb(przyrzad[v]); next_to_queue++; } else if (deadline == ti) { vector<int> old_batch = batch; batch = vector<int>(); deadline = INF; FOREACH(it, old_batch) { int v = queued[*it].top(); queued[*it].pop(); schedule[v] = ti; if (!queued[*it].empty()) { batch.pb(*it); deadline = min(deadline, last_possible[queued[*it].top()]); } } ti++; } else { int next = deadline; if (next_to_queue < N) next = min(deadline, start_and_num[next_to_queue].st); ti = next; } } set<int> all; REP(i,N) all.insert(schedule[i]); printf("%d\n", all.size()); REP(i,N) printf("%d\n", schedule[i]); }
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> #include <bitset> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second typedef bool(*Comparator)(int, int); list<int> reservations[1000000]; int a[1000000], b[1000000], przyrzad[1000000]; int last_possible[1000000]; int schedule[1000000]; const int INF = 2000000000; bool compare_last(int i, int j) { if (a[i] != a[j]) return a[i] < a[j]; else if (b[i] != b[j]) return b[i] < b[j]; else return i < j; } bool compare_first(int i, int j) { return last_possible[i] > last_possible[j]; } struct CompareFirst { bool operator()(const int i, const int j) { return compare_first(i, j); } }; priority_queue<int, vector<int>, CompareFirst> queued[1000000]; int main() { int N, K; scanf("%d%d", &N, &K); map<int, int> mapping; REP(i,N) { int c; scanf("%d%d%d", &a[i], &b[i], &c); if (mapping.find(c) == mapping.end()) { int S = mapping.size(); mapping[c] = S; } przyrzad[i] = mapping[c]; reservations[przyrzad[i]].pb(i); } K = mapping.size(); REP(k,K) { vector<PII> end_and_num; FOREACH(it, reservations[k]) end_and_num.pb(mp(b[*it], *it)); sort(end_and_num.rbegin(), end_and_num.rend()); int o = 0; int ti = end_and_num[0].st; priority_queue<int, vector<int>, Comparator> Q(compare_last); while (o < end_and_num.size() || Q.size() > 0) { if (o < end_and_num.size() && end_and_num[o].st >= ti) { Q.push(end_and_num[o].nd); o++; continue; } else if (!Q.empty()) { int v = Q.top(); Q.pop(); if (ti < a[v]) { printf("NIE\n"); return 0; } last_possible[v] = ti; --ti; } else { ti = end_and_num[o].st; } } } vector<PII> start_and_num; REP(i,N) start_and_num.pb(mp(a[i], i)); int next_to_queue = 0; sort(start_and_num.begin(), start_and_num.end()); vector<int> batch; int deadline = INF; int ti = 0; while (next_to_queue < N || !batch.empty()) { if (next_to_queue < N && start_and_num[next_to_queue].st <= ti) { int v = start_and_num[next_to_queue].nd; queued[przyrzad[v]].push(v); deadline = min(deadline, last_possible[v]); if (queued[przyrzad[v]].size() == 1) batch.pb(przyrzad[v]); next_to_queue++; } else if (deadline == ti) { vector<int> old_batch = batch; batch = vector<int>(); deadline = INF; FOREACH(it, old_batch) { int v = queued[*it].top(); queued[*it].pop(); schedule[v] = ti; if (!queued[*it].empty()) { batch.pb(*it); deadline = min(deadline, last_possible[queued[*it].top()]); } } ti++; } else { int next = deadline; if (next_to_queue < N) next = min(deadline, start_and_num[next_to_queue].st); ti = next; } } set<int> all; REP(i,N) all.insert(schedule[i]); printf("%d\n", all.size()); REP(i,N) printf("%d\n", schedule[i]); } |