File tree 1 file changed +46
-0
lines changed
CSES/Sorting_and_Searching
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ written by Pankaj Kumar.
3
+ country:-INDIA
4
+ */
5
+ #include < bits/stdc++.h>
6
+ using namespace std ;
7
+ typedef long long ll;
8
+
9
+
10
+ int solve (){
11
+ int n,k,ans=0 ;
12
+ cin>>n>>k;
13
+ vector<pair<int ,int > > v (n);
14
+ for (int i=0 ;i<n;i++){
15
+ cin>>v[i].second >>v[i].first ;
16
+ }
17
+ sort (v.begin (),v.end ());
18
+ multiset<int > end_time;
19
+ for (int i=0 ;i<k;i++){
20
+ end_time.insert (0 );
21
+ }
22
+
23
+ for (int i=0 ;i<n;i++){
24
+ // auto it = upper_bound(end_time.begin(),end_time.end(),v[i].second); // O(N) complexity
25
+ auto it = end_time.upper_bound (v[i].second ); // O(log(N)) complexity
26
+ if (it==end_time.begin ()){
27
+ continue ;
28
+ // no one if free
29
+ }
30
+ end_time.erase (--it);
31
+ ans++;
32
+ end_time.insert (v[i].first );
33
+ }
34
+
35
+ cout<<ans<<endl;
36
+ return 0 ;
37
+ }
38
+ int main ()
39
+ {
40
+ int testCase=1 ;
41
+ // cin>>testCase;
42
+ while (testCase--){
43
+ solve ();
44
+ }
45
+ return 0 ;
46
+ }
You can’t perform that action at this time.
0 commit comments