Min Races
Memory limit: 256 MB
In a racing championship there are N racing drivers. The drivers are divided in K classes.
After a race, a driver is declared a winner if all the drivers that finish in front of him are from better classes (with smaller indices than his own).
As the main organiser of the championship, you can choose for each race which drivers are allowed to participate. Find the minimum number of races needed such that every driver is a winner at least once.
Standard input
The first line contains 2 integers N and K.
Each of the next N lines contains 2 integers a_i and b_i. The first integer a_i represents the class of driver i, while b_i is his place in a race with all the N drivers.
Standard output
Print the answer on the first line.
Constraints and notes
- 1≤K≤N≤105
- There will be at least one driver from each class
- The values bb will be a permutation from 1 to N.
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100000+10
struct per{int a,b;}p[MAXN];
int n,k,d[MAXN];
bool cmp(per x,per y){return x.b<y.b;}
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)scanf("%d%d",&p[i].a,&p[i].b);
sort(p+,p+n+,cmp);
d[]=p[].a;
int len=;
for(int i=;i<=n;i++){
if(d[len]>=p[i].a)d[++len]=p[i].a;
else{
int l=,r=len,mid,ans=-;
while(l<=r){
mid=(l+r)>>;
if(d[mid]<p[i].a){
ans=mid;
r=mid-;
}
else l=mid+;
}
d[ans]=p[i].a;
}
}
printf("%d\n",len);
return ;
}