#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int maxl = 20;
const int maxn = 100010;
const int inf = 1000000010;
class SparseTable
{
public:
void build(int n, vector<int> v, bool mn = false)
{
isMin = mn;
for(int i = 1 ; i <= n ; i++)
tab[0][i] = (mn ? -v[i] : v[i]);
for(int k = 1 ; k < maxl ; k++)
for(int i = 1 ; i + (1 << k) - 1 <= n ; i++)
tab[k][i] = max( tab[k - 1][i] , tab[k - 1][i + (1 << (k - 1))] );
}
int query(int l, int r)
{
if( l > r )
return (isMin ? inf : -inf);
int t = 31 - __builtin_clz(r - l + 1);
int ans = max( tab[t][l] , tab[t][r - (1 << t) + 1] );
return (isMin ? -ans : ans);
}
protected:
bool isMin;
int tab[maxl][maxn];
};
int n;
int v[maxn];
int ord[maxn];
SparseTable stV;
vector<int> valleys;
map<int,int> ans;
pair<int,int> getCost(int i, int j)
{
int peak = stV.query( i + 1 , j - 1 );
int diff = min( peak - v[i] , peak - v[j] );
return {diff, i};
}
void findValleys()
{
v[0] = v[n + 1] = inf;
for(int i = 1 ; i <= n ; i++)
if( v[i - 1] > v[i] && v[i] < v[i + 1] ) valleys.push_back( i );
int num = (int)valleys.size();
iota( ord , ord + num , 0 );
sort( ord , ord + num , [&](int i, int j){
return v[ valleys[i] ] > v[ valleys[j] ];
});
multiset<pair<int,int>> allDiffs;
for(int i = 0 ; i < (int)valleys.size() - 1 ; i++)
allDiffs.insert( getCost( valleys[i] , valleys[i + 1] ) );
set<int> alive;
for(int a: valleys)
alive.insert( a );
for(int i = 0 ; i < num - 1 ; i++)
{
int t = allDiffs.begin()->first;
ans[t] = max( ans[t] , num - i );
int A = allDiffs.begin()->second;
int B = *alive.upper_bound(A);
int die = (v[A] < v[B] ? B : A);
auto it = alive.find(die);
auto tmp = alive.begin();
if( it != alive.begin() )
{
tmp = it; tmp--;
allDiffs.erase( allDiffs.find( getCost(*tmp, *it) ) );
}
tmp = it; tmp++;
if( tmp != alive.end() )
allDiffs.erase( allDiffs.find( getCost( *it , *tmp ) ) );
alive.erase(die);
it = alive.lower_bound(die);
if( it != alive.end() && it != alive.begin() )
{
tmp = it; tmp--;
allDiffs.insert( getCost(*tmp, *it) );
}
}
}
void init(int N, vector<int> H)
{
n = N;
H.insert( H.begin() , 0 );
stV.build( n , H );
for(int i = 1 ; i <= n ; i++)
v[i] = H[i];
findValleys();
}
int max_towers(int L, int R, int D)
{
auto t = ans.lower_bound(D);
if( t == ans.end() )
return 1;
return t->second;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
386 ms |
4708 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 |
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 |
0 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 |
700 ms |
10916 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 |
277 ms |
2640 KB |
Output is correct |
2 |
Correct |
904 ms |
10944 KB |
Output is correct |
3 |
Correct |
834 ms |
10944 KB |
Output is correct |
4 |
Correct |
741 ms |
12736 KB |
Output is correct |
5 |
Correct |
945 ms |
12700 KB |
Output is correct |
6 |
Correct |
785 ms |
12696 KB |
Output is correct |
7 |
Correct |
941 ms |
12700 KB |
Output is correct |
8 |
Correct |
891 ms |
8000 KB |
Output is correct |
9 |
Correct |
858 ms |
7968 KB |
Output is correct |
10 |
Correct |
878 ms |
8008 KB |
Output is correct |
11 |
Correct |
932 ms |
7976 KB |
Output is correct |
12 |
Correct |
78 ms |
10944 KB |
Output is correct |
13 |
Correct |
119 ms |
12696 KB |
Output is correct |
14 |
Correct |
126 ms |
12704 KB |
Output is correct |
15 |
Correct |
23 ms |
8008 KB |
Output is correct |
16 |
Correct |
14 ms |
8008 KB |
Output is correct |
17 |
Correct |
82 ms |
10668 KB |
Output is correct |
18 |
Correct |
77 ms |
11004 KB |
Output is correct |
19 |
Correct |
75 ms |
11036 KB |
Output is correct |
20 |
Correct |
117 ms |
12636 KB |
Output is correct |
21 |
Correct |
114 ms |
12720 KB |
Output is correct |
22 |
Correct |
116 ms |
12692 KB |
Output is correct |
23 |
Correct |
119 ms |
12704 KB |
Output is correct |
24 |
Correct |
14 ms |
7984 KB |
Output is correct |
25 |
Correct |
23 ms |
7964 KB |
Output is correct |
26 |
Correct |
14 ms |
7976 KB |
Output is correct |
27 |
Correct |
15 ms |
7968 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
2 ms |
476 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
2 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
2 ms |
464 KB |
Output is correct |
39 |
Correct |
2 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 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 |
386 ms |
4708 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |