Codechef January Challenge 2014 Bon Appetit
Bon AppetitProblem code: FGFS |
Read problems statements in Mandarin Chinese and Russian.
Our chef has recently opened a new restaurant with a unique style. The restaurant is divided into K compartments (numbered from 1 to K) and each compartment can be occupied by at most one customer.
Each customer that visits the restaurant has a strongly preferred compartment p (1 ≤ p ≤ K), and if that compartment is already occupied, then the customer simply leaves. Now obviously, the chef wants to maximize the total number of customers that dine at his restaurant and so he allows (or disallows) certain customers so as to achieve this task. You are to help him with this.
Given a list of N customers with their arrival time, departure time and the preferred compartment, you need to calculate the maximum number of customers that can dine at the restaurant.
Input
The first line contains an integer T denoting the number of test cases. Each of the next T lines contains two integers N and K , the number of customers that plan to visit the chef's restaurant and the number of compartments the restaurant is divided into respectively. Each of the next N lines contains three integers si, fi and pi , the arrival time, departure time and the strongly preferred compartment of the ith customer respectively.
Note that the ith customer wants to occupy the pith compartment from [si, fi) i.e the ith customer leaves just before fi so that another customer can occupy that compartment from fi onwards.
Output
For every test case, print in a single line the maximum number of customers that dine at the restaurant.
Constraints
- 1 ≤ T ≤ 30
- 0 ≤ N ≤ 105
- 1 ≤ K ≤ 109
- 0 ≤ si < fi ≤ 109
- 1 ≤ pi ≤ K
Example
Input: 2 3 3 1 3 1 4 6 2 7 10 3 4 2 10 100 1 100 200 2 150 500 2 200 300 2 Output: 3 3
Explanation
Example case 1.
All three customers want different compartments and hence all 3 can be accommodated.
Example case 2.
If we serve the 1st, 2nd and 4th customers, then we can get a maximum of 3.
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <stack> 6 #include <queue> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define INF 0x7fffffff 12 #define mod 1000000007 13 #define ll long long 14 #define maxn 1000006 15 #define pi acos(-1.0) 16 #define FF(i,n) for(int i=0;i<n;i++) 17 int n, m, z, t, k, flag; 18 struct node{ 19 int l, r, p; 20 }a[maxn],c[maxn],d[maxn],f[maxn]; 21 struct nod{ 22 int x, p; 23 }b[maxn]; 24 bool comp(nod a, nod b){ return a.x < b.x; } 25 bool cmp(node a, node b){ return a.l < b.l; } 26 int main(){ 27 scanf("%d", &t); 28 while (t--){ 29 scanf("%d%d", &n, &k); 30 for (int i = 0; i < n; i++){ 31 scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].p); 32 b[i].x = a[i].p-1; b[i].p = i; 33 } 34 sort(b, b + n,comp); 35 int j = 0, s = 0; 36 for (int i = 0; i < n; i=j){ 37 z = 0; 38 while (j < n&&b[i].x == b[j].x)c[z++] = a[b[j++].p]; 39 sort(c, c + z,cmp); 40 m = 1; d[0] = c[z - 1]; 41 int xx = c[z - 1].r; 42 for (int x = z - 2; x >= 0; x--)if (c[x].r < xx){ 43 xx = c[x].r; 44 d[m++] = c[x]; 45 } 46 sort(d, d + m,cmp); 47 int sz = 0; f[0] = d[0]; 48 int jj = 0; 49 node p; 50 for (int x = 0; x < m; x=jj){ 51 p = d[x]; 52 while (jj<m&&d[x].l == d[jj].l){ 53 if (d[jj].r < p.r)p = d[jj]; 54 jj++; 55 } 56 f[sz++] = p; 57 } 58 int y = 1, tmp = f[0].r; 59 for (int x = 1; x < m; x++) 60 if (f[x].l >= tmp)tmp = f[x].r,y++; 61 s += y; 62 } 63 printf("%d\n", s); 64 } 65 return 0; 66 }
浙公网安备 33010602011771号