이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*************************************
* author: Pham Huy Khanh *
*************************************/
#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FOD(i,b,a) for (int i = (b), _a = (a); i >= _a; i--)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define CNTBIT(x) __builtin_popcountll(x)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define left ___left
#define right ___right
#define pii pair<int, int>
#define DEBUG(n, a) FOR (i, 1, n) cout << a[i] << ' '; cout << endl;
#define endl "\n"
#define NAME "main"
using namespace std;
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y){ x = y; return true; } return false; }
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y){ x = y; return true; } return false; }
#define gogobruhbruh ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
const int MOD = 1e9 + 7;
void add(int &a, int b){ if ((a += b) >= MOD) a -= MOD; }
void sub(int &a, int b){ if ((a -= b) < 0) a += MOD; }
int muti(int a, int b){ return (1LL * a * b) % MOD; }
int Pow(int x, int y){ int res = 1; for (; y; y >>= 1){ if (y & 1) res = muti(res, x); x = muti(x, x); } return res; }
const int MAX = 1e5 + 7;
const int K = 1e2 + 7;
const ll INF = 1e18 + 7;
int num, lim;
ll sum[MAX], dp[MAX];
int trace[MAX][K];
struct Line {
ll a, b;
int id;
Line(ll _a, ll _b, int _id){
a = _a; b = _b; id = _id;
}
ll ord(ll x){
return a * x + b;
}
};
struct CHT {
vector<Line> env;
int idx;
void init(){
env.clear();
idx = 0;
}
bool bad(Line a, Line b, Line c){
return (double) (b.b - a.b) * (a.a - c.a) >= (double) (c.b - a.b) * (a.a - b.a);
}
void add(Line newLine){
while (env.size() >= 2 && bad(env[env.size() - 2], env.back(), newLine))
env.pop_back();
env.eb(newLine);
}
Line get(ll x){
if (env.empty())
return Line(-INF, 0, 0);
int sz = env.size();
if (idx >= sz)
idx = sz - 1;
while (idx + 1 < sz && env[idx + 1].ord(x) > env[idx].ord(x))
idx++;
return Line(env[idx].ord(x), 0, env[idx].id);
}
} cht;
void solve(){
FOR (j, 2, lim){
FOR (i, 1, num){
Line DP = cht.get(sum[i]);
trace[i][j] = DP.id;
if (i >= j - 1)
cht.add(Line(sum[i], dp[i] - sum[i] * sum[i], i));
dp[i] = DP.a;
}
cht.init();
}
cout << dp[num] << endl;
ll tong = 0;
FOD (i, lim, 2)
tong += (sum[num] - sum[trace[num][i]]) * sum[trace[num][i]],
num = trace[num][i],
assert(num),
cout << num << ' ';
assert(tong != dp[num]);
}
void read(){
cin >> num >> lim;
lim++;
FOR (i, 1, num)
cin >> sum[i],
sum[i] += sum[i - 1];
}
int main(){
gogobruhbruh
file("main");
int test = 1;
// cin >> test;
while (test--)
read(),
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:122:5: note: in expansion of macro 'file'
122 | file("main");
| ^~~~
sequence.cpp:30:95: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:122:5: note: in expansion of macro 'file'
122 | file("main");
| ^~~~
# | 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... |