Submission #949533

# Submission time Handle Problem Language Result Execution time Memory
949533 2024-03-19T10:22:05 Z Baizho K blocks (IZhO14_blocks) C++14
0 / 100
1 ms 2912 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
  
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
 
// #pragma GCC optimize("Ofast,unroll-loops,fast-math")
// #pragma GCC target("popcnt,avx")
 
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
 
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define pb push_back
 
const int mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353;  // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const int N = ll(1e5)+100;
const int M = ll(2e5) + 100;
const long long inf = 5e18;
//const long double eps = 1e-15L;
 
ll lcm(ll a, ll b) { return (a / __gcd(a,b))*b; }
 
ll binpow(ll a, ll b, ll m) { ll res=1; a %= m; while(b>0){ if(b&1)res=(res * a) % m; a=(a * a) % m; b/=2; } return res%m;}
 
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }

int n, k, a[N], dp[2][N], lo[N], st[30][N];

int get(int l, int r) {
    int j = lo[r - l + 1];
    return max(st[j][l], st[j][r - (1 << j) + 1]);
}

void solve(int l, int r, int optl, int optr) {
    if(l > r) return;
    int mid = (l + r) / 2;
    int pos = -1;
    for(int j = optl; j <= min(mid, optr); j ++) {
        if(dp[1][mid] > dp[0][j - 1] + get(j, mid)) {
            dp[1][mid] = dp[0][j - 1] + get(j, mid);
            pos = j;
        }
    }
    solve(l, mid - 1, optl, pos); solve(mid + 1, r, pos, optr);
}

void Baizho() {
    for(int i = 2; i < N; i ++) lo[i] = lo[i / 2] + 1;
    cin>>n>>k;
    for(int i = 1; i <= n; i ++) {
        cin>>a[i];
        st[0][i] = a[i];
    }
    for(int j = 1; j <= 17; j ++) {
        for(int i = 1; i + (1 << j) - 1 <= n; i ++) {
            st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
        }
    }
    for(int i = 1; i <= n; i ++) {
        dp[0][i] = get(1, i);
        dp[1][i] = 2e9;
    }
    dp[0][0] = 2e9;
    for(int kk = 2; kk <= k; kk ++) {
        solve(1, n, 1, n);
        for(int i = 1; i <= n; i ++) {
            dp[0][i] = dp[1][i];
            dp[1][i] = 2e9;
        }
    }
    cout<<dp[0][n]<<"\n";
}
 
signed main() {     
//  Freopen("nondec");
    ios_base::sync_with_stdio(false);   
    cin.tie(0);cout.tie(0); 
    // precalc();
    
    int ttt = 1;
   // cin>>ttt;
 
    for(int i=1; i<=ttt; i++) {Baizho(); }
}

Compilation message

blocks.cpp: In function 'void Baizho()':
blocks.cpp:66:64: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   66 |             st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
      |                                                              ~~^~~
blocks.cpp: In function 'void Freopen(std::string)':
blocks.cpp:35:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:35:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Incorrect 1 ms 2908 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Incorrect 1 ms 2912 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Incorrect 1 ms 2908 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Incorrect 1 ms 2908 KB Output isn't correct
5 Halted 0 ms 0 KB -