// g++ -std=c++14
/*
Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.
- Tooru Oikawa.
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double lld;
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define pii pair<int, int>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r)
{
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}
template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}
template <typename T>
ostream &operator<<(ostream &out, set<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << (*i) << ' ';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << (*i) << ' ';
return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, map<T, V> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << "\n" << (i->first) << ":" << (i->second);
return out;
}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
const int N = 1e5 + 100;
const int K = 102;
const ll inf = 1e18;
const int LOG = 22;
int arr[N], n, k;
ll dp[N][K];
int st[N][LOG];
void pre_process(){
for(int i=0;i<n;i++){
st[i][0] = arr[i + 1];
}
for(int j=1;j<LOG;j++){
for(int i=0;i + (1<<j) <= n;i++){
st[i][j] = max(st[i][j-1],st[i+(1 <<(j-1))][j-1]);
}
}
}
int query(int a,int b){
int k = log2(b-a+1);
return max(st[a][k],st[b - (1<<k) +1][k]);
}
deque<ll> d[K];
int solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
pre_process();
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
dp[i][j] = inf;
}
}
dp[0][0] = 0;
d[0].push_back(0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (j > i) continue;
auto tans = d[j - 1].front();
dp[i][j] = dp[tans][j - 1] + query(tans, i - 1);
}
for (int j = 1; j <= k; j++) {
ll temp = dp[i][j] + arr[i + 1];
while (!d[j].empty() && dp[d[j].back()][j] + (ll)query(d[j].back(), i) > temp) d[j].pop_back();
d[j].push_back(i);
}
}
cout << dp[n][k] << endl;
return 0;
}
int32_t main(){ _
int t;
// cin >> t;
t = 1;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
80376 KB |
Output is correct |
2 |
Correct |
46 ms |
80380 KB |
Output is correct |
3 |
Correct |
51 ms |
80376 KB |
Output is correct |
4 |
Correct |
47 ms |
80384 KB |
Output is correct |
5 |
Correct |
48 ms |
80376 KB |
Output is correct |
6 |
Correct |
53 ms |
80376 KB |
Output is correct |
7 |
Correct |
45 ms |
80340 KB |
Output is correct |
8 |
Correct |
45 ms |
80376 KB |
Output is correct |
9 |
Correct |
46 ms |
80376 KB |
Output is correct |
10 |
Correct |
46 ms |
80376 KB |
Output is correct |
11 |
Incorrect |
57 ms |
80248 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
80376 KB |
Output is correct |
2 |
Correct |
46 ms |
80376 KB |
Output is correct |
3 |
Correct |
46 ms |
80376 KB |
Output is correct |
4 |
Correct |
48 ms |
80296 KB |
Output is correct |
5 |
Correct |
47 ms |
80376 KB |
Output is correct |
6 |
Correct |
48 ms |
80376 KB |
Output is correct |
7 |
Correct |
47 ms |
80376 KB |
Output is correct |
8 |
Correct |
48 ms |
80376 KB |
Output is correct |
9 |
Correct |
46 ms |
80376 KB |
Output is correct |
10 |
Correct |
46 ms |
80376 KB |
Output is correct |
11 |
Incorrect |
46 ms |
80376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
80376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
81912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |