#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
ofstream fout("output.txt");
int n;
vector<int> arr;
vector<int> upidx, downidx;
struct localmax {
int idx, d, l, r;
bool operator<(const localmax &other) const {
if (d == other.d) return idx < other.idx;
return d < other.d;
}
void update() {
d = arr[upidx[idx]] - max(arr[l], arr[r]);
}
} local_value[101010];
int adj[101010][2];
set<localmax> local;
vector< pair<int, int> > Dlist;
void init(int N, vector<int> H) {
n = N;
for (int i = 0; i < N; i++) arr.push_back(H[i]);
if (arr[0] < arr[1]) downidx.push_back(0);
for (int i = 1; i < N - 1; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) upidx.push_back(i);
if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) downidx.push_back(i);
}
if (arr[N - 1] < arr[N - 2]) downidx.push_back(N - 1);
for (int i = 0; i < upidx.size(); i++) {
local_value[i] = {i, 0, downidx[i], downidx[i + 1]};
local_value[i].update();
local.insert(local_value[i]);
adj[i][0] = i - 1;
adj[i][1] = i + 1;
}
while (!local.empty()) {
localmax now = *local.begin();
Dlist.push_back({now.d, upidx[now.idx]});
int next = (arr[now.l] < arr[now.r] ? now.l : now.r);
local.erase(local.begin());
if (adj[now.idx][0] >= 0) {
set<localmax>::iterator iter = local.find(local_value[adj[now.idx][0]]);
if (iter != local.end()) {
local.erase(iter);
local_value[adj[now.idx][0]].r = next;
local_value[adj[now.idx][0]].update();
local.insert(local_value[adj[now.idx][0]]);
adj[adj[now.idx][0]][1] = adj[now.idx][1];
}
}
if (adj[now.idx][1] < upidx.size()) {
set<localmax>::iterator iter = local.find(local_value[adj[now.idx][1]]);
if (iter != local.end()) {
local.erase(iter);
local_value[adj[now.idx][1]].l = next;
local_value[adj[now.idx][1]].update();
local.insert(local_value[adj[now.idx][1]]);
adj[adj[now.idx][1]][0] = adj[now.idx][0];
}
}
}
// for (int i = 0; i < Dlist.size(); i++) cout << Dlist[i].first << " " << Dlist[i].second << "\n";
}
int max_towers(int L, int R, int D) {
int ans = Dlist.end() - lower_bound(Dlist.begin(), Dlist.end(), make_pair(D, 0)) + 1;
return ans;
}
Compilation message
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:36:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < upidx.size(); i++) {
| ~~^~~~~~~~~~~~~~
towers.cpp:59:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | if (adj[now.idx][1] < upidx.size()) {
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
241 ms |
1076 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
739 ms |
5284 KB |
1st lines differ - on the 1st token, expected: '11903', found: '33010' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
292 ms |
1560 KB |
Output is correct |
2 |
Correct |
934 ms |
5284 KB |
Output is correct |
3 |
Correct |
944 ms |
5428 KB |
Output is correct |
4 |
Correct |
830 ms |
6960 KB |
Output is correct |
5 |
Correct |
1008 ms |
6980 KB |
Output is correct |
6 |
Correct |
758 ms |
6960 KB |
Output is correct |
7 |
Correct |
909 ms |
6976 KB |
Output is correct |
8 |
Correct |
706 ms |
1692 KB |
Output is correct |
9 |
Correct |
683 ms |
1704 KB |
Output is correct |
10 |
Correct |
784 ms |
1604 KB |
Output is correct |
11 |
Correct |
719 ms |
1720 KB |
Output is correct |
12 |
Correct |
48 ms |
5312 KB |
Output is correct |
13 |
Correct |
77 ms |
6896 KB |
Output is correct |
14 |
Correct |
70 ms |
6952 KB |
Output is correct |
15 |
Correct |
17 ms |
1712 KB |
Output is correct |
16 |
Correct |
12 ms |
1688 KB |
Output is correct |
17 |
Correct |
41 ms |
4924 KB |
Output is correct |
18 |
Correct |
46 ms |
5360 KB |
Output is correct |
19 |
Correct |
52 ms |
5312 KB |
Output is correct |
20 |
Correct |
70 ms |
6928 KB |
Output is correct |
21 |
Correct |
63 ms |
6844 KB |
Output is correct |
22 |
Correct |
65 ms |
6940 KB |
Output is correct |
23 |
Correct |
71 ms |
6928 KB |
Output is correct |
24 |
Correct |
12 ms |
1712 KB |
Output is correct |
25 |
Correct |
12 ms |
1704 KB |
Output is correct |
26 |
Correct |
11 ms |
1716 KB |
Output is correct |
27 |
Correct |
11 ms |
1708 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
241 ms |
1076 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |