#include <bits/stdc++.h>
#include <random>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define o cout<<"BUG"<<endl;
#define IOS ios_base::sync_with_stdio(0);
#define en "\n"
#define FOR(i, j, n) for(int j = i; j < n; ++j)
#define forn(i, j, n) for(int j = i; j <= n; ++j)
#define nfor(i, j, n) for(int j = n; j >= i; --j)
#define sortv(vv) sort(vv.begin(), vv.end())
#define all(v) v.begin(), v.end()
#define ld long double
#define ull unsigned long long
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
const ll maxn=1e5+100, inf=1e18,LOG=18,mod=1e9+7;
ll block = 300, timer = 0;
const ld EPS = 1e-7;
#define bt(i) (1 << (i))
#define int ll
#define y1 yy
#define pii pair <int, int>
int n, k, a[maxn], step[maxn];
pii dp[maxn];
int parse[maxn][LOG];
int calc(int l, int r)
{
int len = r - l + 1;
//7 8 9 10 11
return max(parse[l][step[len]], parse[r - (1 << step[len]) + 1][step[len]]);
}
stack <pair <int, pair <int, pii> > > st;
multiset <pii> sett;
int check(int pen, int ty)
{
while(st.size()) st.pop();
sett.clear();
dp[0] = {0, 0};
forn(1, i, n)
{
dp[i] = {inf, inf};
//dp[i] = min(dp[i - 1], dp[j - 1] + calc(j, i) - pen (1 <= j <= i));
auto upd = mp(a[i], mp(i, dp[i - 1]));
while(st.size() && st.top().f <= a[i])
{
auto was = st.top();
sett.erase(sett.find({was.s.s.f + was.f, was.s.s.s}));
st.pop();
upd.s.f = was.s.f;
upd.s.s = min(upd.s.s, was.s.s);
}
st.push(upd);
sett.insert({upd.f + upd.s.s.f, upd.s.s.s});
pii temp = *sett.begin();
if(dp[i] > mp(temp.f - pen, temp.s + 1))
{
dp[i] = {temp.f - pen, temp.s + 1};
}
// cout << dp[i].f << " " << dp[i].s << endl;
}
if(ty == 1)
return dp[n].s;
else
return dp[n].f + dp[n].s * pen;
}
main()
{
IOS
cin >> n >> k;
step[1] = 0;
forn(2, i, n)
{
step[i] = step[i - 1];
if((1 << (step[i] + 1)) <= i)
{
step[i]++;
}
}
forn(1, i, n)
{
cin >> a[i];
parse[i][0] = a[i];
}
forn(1, j, LOG - 1)
{
forn(1, i, n)
{
if(i + (1 << (j - 1)) <= n)
parse[i][j] = max(parse[i][j - 1], parse[i + (1 << (j - 1))][j - 1]);
else
parse[i][j] = parse[i][j - 1];
}
}
// cout << check(2, 1);
// exit(0);
int l = 0;
int r = inf;
while(r - l > 1)
{
int mid = (l + r) / 2;
if(check(mid, 1) > k)
{
r = mid;
}
else
{
l = mid;
}
}
int rb = r - 1;
l = 0;
r = inf;
while(r - l > 1)
{
int mid = (l + r) / 2;
if(check(mid, 1) >= k)
{
r = mid;
}
else
{
l = mid;
}
}
int lb = r;
// cout << lb << " " << rb << endl;
// cout << check(2, 1) << endl;
int ans = inf;
forn(lb, i, rb)
{
ans = min(ans, check(i, 2));
}
cout << ans;
}
Compilation message
blocks.cpp:82:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1095 ms |
384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
82 ms |
2204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |