# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
555834 |
2022-05-01T16:13:59 Z |
urosk |
K blocks (IZhO14_blocks) |
C++14 |
|
1 ms |
340 KB |
// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;
#define maxn 100005
#define maxk 105
#define lg 25
ll n,k;
ll a[maxn];
ll st[maxn][lg];
ll powi[maxn];
bool test = 0;
ll get(ll l,ll r){
ll len = (r-l+1);
ll k = powi[len];
ll ans = max(st[l][k],st[r-(1<<k)+1][k]);
if(test){
ll g =0 ;
for(ll i = l;i<=r;i++) g = max(g,a[i]);
if(g!=ans){
cerr<<"MIN: "<<l<< " "<<r<<endl;
exit(0);
}
}
return ans;
}
ll brut(){
vector<vector<ll> > v(n+1,vector<ll>(k+1,0));
v[0][0] = 0;
for(ll i = 1;i<=n;i++) v[i][0] = llinf;
for(ll j = 1;j<=k;j++){
v[0][j] = llinf;
for(ll i = 1;i<=n;i++){
v[i][j] = llinf;
ll mx = a[i];
for(ll f = i-1;f>=0;f--){
v[i][j] = min(v[i][j],v[f][j-1] + mx);
mx = max(mx,a[f]);
}
}
for(ll i = 0;i<=n;i++) cerr<<v[i][j]<< " ";
cerr<<endl;
}
return v[n][k];
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
bool gen = 1;
ll b[maxn];
ll dp[maxn][2];
ll dpmin[maxn][2];
ll opt[maxn];
void calc(ll i,ll id){
if(b[i]==0){
opt[i] = dpmin[i-1][id] + a[i];
return;
}
calc(b[i],id);
opt[i] = min(opt[b[i]],dp[b[i]][id] + a[i]);
}
int main(){
test = 0;
gen = 0;
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
if(gen==0){
cin >> n >> k;
for(ll i = 1;i<=n;i++) cin >> a[i];
}else{
n = 500;
k = 4;
for(ll i = 1;i<=n;i++) a[i] = rnd(1,5);
cerr<<n<< " "<<k<<endl;
for(ll i = 1;i<=n;i++) cerr<<a[i]<< " ";
cerr<<endl;
}
for(ll i = 1;i<=n;i++) st[i][0] = a[i];
for(ll j = 1;j<lg;j++){
for(ll i = 1;i+(1<<j)-1<=n;i++){
st[i][j] = max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
for(ll j = 0;j<lg;j++){
if((1<<j)>n) break;
powi[(1<<j)] = j;
}
for(ll i = 1;i<=n;i++){
if(powi[i]==0) powi[i] = powi[i-1];
}
stack<ll> st;
for(ll i =1;i<=n;i++){
while(sz(st)&&a[i]>a[st.top()]) st.pop();
if(sz(st)) b[i] = st.top();
st.push(i);
}
ceri(b,1,n);
for(ll i = 1;i<=n;i++) dp[i][0] = llinf;
dp[0][0] = 0;
for(ll i = 1;i<=n;i++) dpmin[i][0] = 0;
for(ll j = 1;j<=k;j++){
for(ll i = 1;i<=n;i++) opt[i] = llinf;
dp[0][j] = llinf;
for(ll i = 1;i<=n;i++){
calc(i,(j&1)^1);
dp[i][j&1] = opt[i];
}
/*
here;
ceri(opt,1,n);
for(ll i = 0;i<=n;i++) cerr<<dp[i][j&1]<< " ";
cerr<<endl;
for(ll i = 0;i<=n;i++) cerr<<dp[i][(j&1)^1]<< " ";
cerr<<endl;
for(ll i = 0;i<n;i++) cerr<<dpmin[i][(j&1)^1]<<" ";
cerr<<endl;
*/
dpmin[0][(j&1)] = dp[0][j&1];
for(ll i = 1;i<=n;i++) dpmin[i][j&1] = min(dpmin[i-1][j&1],dp[i][j&1]);
for(ll i = 0;i<=n;i++) dp[i][(j&1)^1] = dp[i][j&1];
for(ll i = 0;i<=n;i++) dpmin[i][(j&1)^1] = dpmin[i][j&1];
//for(ll i = 0;i<=n;i++) dp[i][(j&1)] = llinf;
}
//here;
if(test) cerr<<"brut: "<<brut()<<endl;
cout<<dp[n][(k&1)^1]<<endl;
return 0;
}
/*
5 2
1 2 3 4 5
out: 6
5 4
3 1 3 5 1
out: 10
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |