Submission #265535

#TimeUsernameProblemLanguageResultExecution timeMemory
265535square1001Split the sequence (APIO14_sequence)C++14
100 / 100
684 ms83168 KiB
// APIO 2014 Problem 2 - Split the Sequence #include <vector> #include <iostream> #include <algorithm> using namespace std; const long long inf = 1LL << 62; int main() { // step #1. read input cin.tie(0); ios_base::sync_with_stdio(false); int N, K; cin >> N >> K; ++K; vector<int> A(N); for(int i = 0; i < N; ++i) { cin >> A[i]; } // step #2. preparation for calculating the answer vector<int> S(N + 1); for(int i = 0; i < N; ++i) { S[i + 1] = S[i] + A[i]; } // step #3. calculate the answer with dynamic programming vector<long long> dp(N + 1, inf); vector<vector<int> > pre(K + 1, vector<int>(N + 1, -1)); dp[0] = 0; for(int i = 1; i <= K; ++i) { vector<int> st; for(int j = 0; j <= N; ++j) { while(st.size() >= 2) { int sz = st.size(); __int128_t lc = __int128_t(dp[j] - dp[st[sz - 1]]) * (S[j] - S[st[sz - 2]]); __int128_t rc = __int128_t(dp[j] - dp[st[sz - 2]]) * (S[j] - S[st[sz - 1]]); if(lc <= rc) st.pop_back(); else break; } st.push_back(j); } vector<long long> ndp(N + 1, inf); int pos = 0; for(int j = 1; j <= N; ++j) { while(pos + 1 != st.size()) { long long la = dp[st[pos]] - 1LL * S[st[pos]] * S[j]; long long lb = dp[st[pos + 1]] - 1LL * S[st[pos + 1]] * S[j]; if(la > lb) ++pos; else break; } long long x = dp[st[pos]] - 1LL * S[st[pos]] * S[j]; if(ndp[j] > x) { ndp[j] = x; pre[i][j] = st[pos]; } ndp[j] += 1LL * S[j] * S[j]; } dp = ndp; } // step #4. print the answer cout << 1LL * S[N] * S[N] - dp[N] << endl; int pos = N; vector<bool> sol(N + 1, false); int cnt = 0; for(int i = K; i >= 2; --i) { pos = pre[i][pos]; if(!sol[pos]) { sol[pos] = true; ++cnt; } } for(int i = 1; i < N; ++i) { if(cnt < K - 1 && !sol[i]) { sol[i] = true; ++cnt; } } for(int i = 1; i < N; ++i) { if(sol[i]) { cout << i << (--cnt ? ' ' : '\n'); } } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:42:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |    while(pos + 1 != st.size()) {
      |          ~~~~~~~~^~~~~~~~~~~~
#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...