이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define For(i,N) Forr(i, 0, N)
#define Forr(i,a,b) Fotr(i, a, b, 1)
#define Fotr(i,a,b,c) for(int i=(a);i<(b);i+=(c))
#define FOREACH(it, x) for(__typeof__((x).begin()) it=(x).begin();it!=(x).end();it++)
#define MEM(a,x) memset(a,x,sizeof(a))
#define BCHK(a,x) (((a)>>(x))&1)
#define BSET(a,x) ((a)|(1<<(x)))
#define BCLR(a,x) ((a)&(~(1<<(x))))
#define BTGL(a,x) ((a)^(1<<(x)))
#define FMT(...) (sprintf(CRTBUFF, __VA_ARGS__)?CRTBUFF:0)
#define read() freopen("input.txt","r",stdin)
#define write() freopen("output.txt","w",stdout)
#define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);}
#define BUFFSIZE 30000
#define INF 1000000000
#define MOD 1000000007
#define MAX 100100
#define pb push_back
#define mkpr make_pair
#define pii pair<int, int>
#define fi first
#define si second
typedef long long ll;
char CRTBUFF[BUFFSIZE];
#define dbg(args...) {cout<<"-->";debugger::call(#args,args);cout<<"\n";}
struct debugger{
static void call(const char* it){}
template<typename T, typename ... aT>
static void call(const char* it, T a, aT... rest){
string b;
for(;*it&&*it!=',';++it) if(*it!=' ')b+=*it;
cout<<b<<"="<<a<<" ";
call(++it, rest...);
}
};
/* Template Ends */
struct CHT{
vector<ll> m, b, id;
int ptr = 0;
bool bad(int l1, int l2, int l3) {
return (b[l3] - b[l1]) * (m[l1] - m[l2]) <= (b[l2] - b[l1]) * (m[l1] - m[l3]);
}
void clear() {
ptr = 0;
m.clear();
b.clear();
id.clear();
}
void add(ll _m, ll _b, ll idx) {
m.push_back(_m);
b.push_back(_b);
id.push_back(idx);
int s = m.size();
while(s >= 3 && bad(s-3, s-2, s-1)) {
s--;
m.erase(m.end()-2);
b.erase(b.end()-2);
id.erase(id.end()-2);
}
}
ll f(int i, ll x) { return m[i]*x + b[i]; }
pair<ll,ll> query(ll x) {
if(ptr >= m.size()) ptr = m.size()-1;
while(ptr < m.size()-1 &&
f(ptr+1, x) >= f(ptr, x)) ptr++;
return {f(ptr, x), id[ptr]};
}
};
ll A[MAX+10], S[MAX+10];
ll dp[2][MAX+10];
int fd[202][MAX+10];
int main()
{
//read();
cpp_io();
//begin
int n, k;
while(cin >> n >> k){
For(i, n) cin >> A[i];
For(i, n) S[i]=A[i]+(i?S[i-1]:0);
For(j, n) dp[0][j]=fd[0][j]=0;
CHT h;
Forr(i, 1, k+1){
h.clear();
h.add(0, 0, 0);
int x=i&1;
For(j, n){
if(j==0){
dp[x][j]=0;
fd[i][j]=0;
}
else{
tie(dp[x][j], fd[i][j])=h.query(S[j]);
}
h.add(S[j], dp[x^1][j]-S[j]*S[j], j);
}
}
ll ans=dp[k&1][n-1];
cout << ans << "\n";
int j=fd[k][n-1];
for(int i=k-1; i>=0; i--){
cout << j+1;
if(i) cout << " ";
j=fd[i][j];
}
cout << "\n";
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'std::pair<long long int, long long int> CHT::query(ll)':
sequence.cpp:73:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ptr >= m.size()) ptr = m.size()-1;
^
sequence.cpp:74:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < m.size()-1 &&
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |