제출 #100124

#제출 시각아이디문제언어결과실행 시간메모리
100124ansol4328수열 (APIO14_sequence)C++11
60 / 100
542 ms132096 KiB
#include<stdio.h>

using namespace std;
typedef long long ll;

struct line
{
    ll slope, constant;
    int num;
    line() : slope(0), constant(0), num(0) {}
    line(ll a, ll b, int c) : slope(a), constant(b), num(c) {}
};

struct CHT
{
    int sz, idx;
    line L[100005];
    CHT() : sz(0), idx(1) {}
	double get_point(line L1, line L2);
	bool increase(line L1, line L2, line L3);
	void update(line L3);
	ll query(ll x);
	int get_line_num();
	void clr();
};

double CHT::get_point(line L1, line L2)
{
    return (double)(L2.constant-L1.constant)/(L1.slope-L2.slope);
}
bool CHT::increase(line L1, line L2, line L3)
{
    double p1=CHT::get_point(L1,L2);
    double p2=CHT::get_point(L2,L3);
    return p1<=p2;
}
void CHT::update(line L3)
{
    while(sz>1)
    {
        line L2=L[sz--];
        line L1=L[sz];
        if(CHT::increase(L1,L2,L3))
        {
            sz++;
            break;
        }
    }
    L[++sz]=L3;
}
ll CHT::query(ll x)
{
    while(sz-idx+1>1 && CHT::get_point(L[idx],L[idx+1])<(double)x)
    {
        idx++;
    }
    line L1=L[idx];
    return L1.slope*x+L1.constant;
}
int CHT::get_line_num()
{
    line L1=L[idx];
    return L1.num;
}
void CHT::clr()
{
    idx=1, sz=0;
}

int n, k;
ll sum[100005];
ll dp[2][100005], path[205][100005];
CHT cht[2];

int main()
{
    scanf("%d %d",&n,&k);
    for(int i=1 ; i<=n ; i++)
    {
        scanf("%lld",&sum[i]);
        sum[i]+=sum[i-1];
    }
    k++;
    cht[0].update(line(0,0,0));
    for(int i=1 ; i<=k ; i++)
    {
        int prev=(i-1)%2;
        int cur=i%2;
        cht[cur].clr();
        for(int j=1 ; j<=n ; j++)
        {
            ll val=cht[prev].query(sum[j]);
            dp[cur][j]=val;
            path[i][j]=cht[prev].get_line_num();
            cht[cur].update(line(sum[j],dp[cur][j]-sum[j]*sum[j],j));
        }
    }
    printf("%lld\n",dp[k%2][n]);
    int res[205], cnt=k;
    res[k]=n;
    while(cnt!=1)
    {
        res[cnt-1]=path[cnt][res[cnt]];
        cnt--;
    }
    for(int i=1 ; i<k ; i++) printf("%d ",res[i]);
    return 0;
}

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

sequence.cpp: In function 'int main()':
sequence.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&k);
     ~~~~~^~~~~~~~~~~~~~~
sequence.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&sum[i]);
         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...