#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 1;
typedef pair<int, int> pi;
struct Doll {
int diameter;
int height;
};
bool operator < (const Doll& a, const Doll& b)
{
if (a.height == b.height)
return a.diameter > b.diameter;
return a.height < b.height;
}
int N, Q;
vector<Doll> dolls;
vector<pair<pi, int>> queries;
// dp[i]: Maximum diameter of matryoshka doll at each time step
vector<int> dp;
vector<int> ans;
bool gcomp(const int& a, const int& b)
{
return a > b;
}
void process_query(int v)
{
// Only take dolls with diameter as required
int res = upper_bound(dp.begin(), dp.end(), queries[v].first.first, gcomp) - dp.begin();
// All dolls at position res and above have diameter smaller than required queries[cur_]
ans[queries[v].second] = res - 1;
}
int main(void)
{
scanf("%d %d", &N, &Q);
dolls.resize(N);
queries.resize(Q);
ans.resize(Q);
dp.resize(N + 1, 0);
for (int i = 0; i < N; i++) {
scanf("%d %d", &dolls[i].diameter, &dolls[i].height);
}
for (int i = 0; i < Q; i++) {
scanf("%d %d", &queries[i].first.first, &queries[i].first.second);
queries[i].second = i;
}
sort(dolls.begin(), dolls.end());
sort(queries.begin(), queries.end(), [] (pair<pi, int> a, pair<pi, int> b) {return a.first.second < b.first.second;});
int reached = 0;
dp[0] = INF;
int cur_query = 0;
for (int i = 0; i < N; i++) {
while (cur_query < Q && queries[cur_query].first.second < dolls[i].height) {
process_query(cur_query);
cur_query++;
}
/* A pair of dolls (i, j) after sorting do NOT fit inside one another iff
* i < j => diameter(i) >= diameter(j)
*/
int pos = upper_bound(dp.begin(), dp.end(), dolls[i].diameter, gcomp) - dp.begin();
dp[pos] = dolls[i].diameter;
reached = max(reached, pos);
}
while (cur_query < Q) {
process_query(cur_query);
cur_query++;
}
for (int i = 0; i < Q; i++)
printf("%d\n", ans[i]);
return 0;
}
Compilation message
matryoshka.cpp: In function 'int main()':
matryoshka.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d %d", &N, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~
matryoshka.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d %d", &dolls[i].diameter, &dolls[i].height);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
matryoshka.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d %d", &queries[i].first.first, &queries[i].first.second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
304 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
304 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
304 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
304 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
444 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
316 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
440 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
304 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
304 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
444 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
316 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
440 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
130 ms |
11664 KB |
Output is correct |
37 |
Correct |
124 ms |
10972 KB |
Output is correct |
38 |
Correct |
45 ms |
4684 KB |
Output is correct |
39 |
Correct |
106 ms |
9348 KB |
Output is correct |
40 |
Correct |
132 ms |
9856 KB |
Output is correct |
41 |
Correct |
122 ms |
10828 KB |
Output is correct |
42 |
Correct |
101 ms |
8576 KB |
Output is correct |
43 |
Correct |
85 ms |
8716 KB |
Output is correct |
44 |
Correct |
137 ms |
14256 KB |
Output is correct |
45 |
Correct |
137 ms |
14216 KB |
Output is correct |
46 |
Correct |
139 ms |
14248 KB |
Output is correct |
47 |
Correct |
132 ms |
13972 KB |
Output is correct |
48 |
Correct |
145 ms |
14484 KB |
Output is correct |
49 |
Correct |
130 ms |
13968 KB |
Output is correct |
50 |
Correct |
135 ms |
14392 KB |
Output is correct |