#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;
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;
}
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<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 ; i++)
{
int t = *allDiffs.begin();
ans[t] = max( ans[t] , num - i );
int die = valleys[ ord[i] ];
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
468 ms |
4664 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '85' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '85' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
560 ms |
10908 KB |
1st lines differ - on the 1st token, expected: '11903', found: '33010' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
202 ms |
2640 KB |
1st lines differ - on the 1st token, expected: '7197', found: '425' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '85' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
468 ms |
4664 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |