#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> arr;
vector<int> upidx, downidx;
vector<int> dvalues;
struct localmax {
int idx, d, l, r;
bool operator<(const localmax &other) const {
if (d == other.d) return arr[upidx[idx]] < arr[upidx[other.idx]];
return d < other.d;
}
void update() {
d = arr[upidx[idx]] - max(arr[l], arr[r]);
}
} local_value[101010];
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]);
}
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 (now.idx - 1 >= 0) {
set<localmax>::iterator iter = local.find(local_value[now.idx - 1]);
if (iter != local.end()) {
local.erase(iter);
local_value[now.idx - 1].r = next;
local_value[now.idx - 1].update();
local.insert(local_value[now.idx - 1]);
}
}
if (now.idx + 1 < upidx.size()) {
set<localmax>::iterator iter = local.find(local_value[now.idx + 1]);
if (iter != local.end()) {
local.erase(iter);
local_value[now.idx + 1].l = next;
local_value[now.idx + 1].update();
local.insert(local_value[now.idx + 1]);
}
}
}
// for (int i = 0; i < upidx.size(); i++) {
// dvalues.push_back(arr[upidx[i]] - max(arr[downidx[i]], arr[downidx[i + 1]]));
// }
// sort(dvalues.begin(), dvalues.end());
// for (int i = 0; i < Dlist.size(); i++) cout << Dlist[i].first << " " << Dlist[i].second << "\n";
// for (int i = 0; i < dvalues.size(); i++) cout << dvalues[i] << "\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:56:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if (now.idx + 1 < upidx.size()) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
399 ms |
1064 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 |
0 ms |
208 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 |
0 ms |
208 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 |
613 ms |
4968 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 |
Incorrect |
276 ms |
1408 KB |
2nd lines differ - on the 1st token, expected: '7063', found: '7060' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
208 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 |
399 ms |
1064 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |