제출 #1365176

#제출 시각아이디문제언어결과실행 시간메모리
1365176eyadoozSplit the sequence (APIO14_sequence)C++20
0 / 100
0 ms344 KiB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define endl '\n'
#define int long long

struct line
{
    int m, b;
    int ind;
    pii calc(int x)
    {
        return {m*x+b, ind};
    }
};
 
struct node
{
    node *l=nullptr, *r=nullptr;
    line *ln=nullptr;
};
void update(node* cur, line* lin, int l, int r)
{
    int m=(l+r)/2;
    if(cur->ln==nullptr) {cur->ln=lin;return;}
    if(lin->calc(m)>cur->ln->calc(m)) swap(lin, cur->ln);
    if(l==r) return;
 
    if(cur->ln->calc(l)<lin->calc(l))
    {
        if(cur->l==nullptr) cur->l = new node();
        update(cur->l, lin, l, m);
    }
    if(cur->ln->calc(r)<lin->calc(r))
    {
        if(cur->r==nullptr) cur->r = new node();
        update(cur->r, lin, m+1, r);
    }
}
pii query(int x, node* cur, int l, int r)
{
    if(!cur||cur->ln==nullptr) return {-LONG_LONG_MAX, 0};
    int m=(l+r)/2;
    if(l==r) return cur->ln->calc(x);
    else if(x<=m) return max(cur->ln->calc(x), query(x, cur->l, l, m));
    else return max(cur->ln->calc(x), query(x, cur->r, m+1, r));
}
node* root[205];

main()
{
    cin.tie(0) -> sync_with_stdio(0);

    for(int i = 0;i <= 200;i++) root[i]=new node();
    int n, k;
    cin >> n >> k;
    int a[n+1], pref[n+5]={}, sum=0;
    for(int i = 1;i <= n;i++) {cin >> a[i];pref[i]=pref[i-1]+a[i];sum+=a[i];}
    int dp[n+5][k+5]={};
    int last[n+5][k+1]={};
    for(int i = 0;i <= n;i++) 
    {
        for(int j=0;j<=k;j++) dp[i][j]=-INT_MAX;
    }
    dp[0][0]=a[0]=0;
    int mx=0;
    last[0][0]=-1;
    update(root[0], new line{0, 0, -1}, 0, 1e9);
    for(int i = 1;i <= n;i++) 
    {
        sum-=a[i];
        for(int x=1;x<=k;x++) 
        {
            if(x>i) continue;
            auto[val, ind]=query(sum, root[x-1], 0, 1e9);
            dp[i][x]=val+(sum*pref[i]);
            last[i][x]=ind;
            if(i==3&&x==k-1) {cout << ind << endl;}
        }
        for(int x=1;x<=k;x++) 
        {
            if(x>i) continue;
            update(root[x], new line{-pref[i], dp[i][x], i}, 0, 1e9);
        }
        mx=max(mx, dp[i][k]);
    }
    for(int i = 1;i<=n;i++) 
    {
        if(mx==dp[i][k]) 
        {
            cout << mx << endl;
            int cur=k, j=i;
            cout << j << " ";
            while(cur!=1) 
            {
                cout << last[j][cur]-1 << " ";
                j=last[j][cur];
                cur--;
            }
            return 0;
        }
    }
}
    

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   57 | main()
      | ^~~~
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…