[Hacker Earth] Legendary Magician

Legendary Magician

  • Time :
  • Space :
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
#include<bits/stdc++.h>
using namespace std;

int solve (int n, vector<int> A, vector<int> B) {
vector<pair<int,int>> S;
for(int i = 0; i < n; i++) S.push_back({A[i],B[i]});
sort(begin(S), end(S));
int res = 0, ma = 0;
for(int i = 0; i < n; i++) {
auto [s,e] = S[i];
if(s > ma) {
res += 1;
ma = e;
} else ma = min(ma,e);
}
return res;
}

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for(int t_i = 0; t_i < T; t_i++)
{
int n;
cin >> n;
vector<int> A(n);
for(int i_A = 0; i_A < n; i_A++)
{
cin >> A[i_A];
}
vector<int> B(n);
for(int i_B = 0; i_B < n; i_B++)
{
cin >> B[i_B];
}

int out_;
out_ = solve(n, A, B);
cout << out_;
cout << "\n";
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/08/16/PS/HackerEarth/legendary-magician/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.