Submission #46313

#TimeUsernameProblemLanguageResultExecution timeMemory
46313RayaBurong25_1Split the sequence (APIO14_sequence)C++14
0 / 100
2064 ms87036 KiB
#include <stdio.h> #include <vector> #include <algorithm> #include <cassert> #define INF 1e18 long long Sum[100005]; long long SumSq[100005]; long long DP[100005][2]; int Last[100005][205]; typedef struct line line; struct line { long long m, c; double s; int i; }; line Lines[100005]; int L; long long max(long long a, long long b) { return (a > b)?a:b; } int compare(long long x, line l) { return x < l.s; } int I, J; long long intersect(long long m, long long c, long long m2, long long c2) { if (m == m2) { if (c2 >= c) return -INF; else return INF; } else return (c2 - c)/(m - m2); } int main() { int N, K; scanf("%d %d", &N, &K); int n, k, i; for (i = 0; i < N; i++) { scanf("%lld", &Sum[i]); if (i > 0) Sum[i] += Sum[i - 1]; SumSq[i] = Sum[i]*Sum[i]; } long long r, s, c; for (k = 1; k <= K; k++) { L = 1; J = 0; Lines[0] = {0, 0, -INF, -1}; for (n = 0; n < N; n++) { while (J + 1 < L && Lines[J + 1].s <= Sum[n]) J++; DP[n][k%2] = Lines[J].m*Sum[n] + Lines[J].c; Last[n][k] = Lines[J].i; c = DP[n][(k - 1)%2] - SumSq[n]; while (L > 0 && (s = intersect(Lines[L - 1].m, Lines[L - 1].c, Sum[n], c)) <= Lines[L - 1].s) L--; if (s != INF) { Lines[L] = {Sum[n], c, s, n}; L++; } } // printf("k%d\n", k); } printf("%lld\n", DP[N - 1][K%2]); i = Last[N - 1][K]; k = K - 1; std::vector<int> V; while (k >= 0 && i != -1) { V.push_back(i + 1); i = Last[i][k]; k--; } for (i = V.size() - 1; i >= 0; i--) printf("%d ", V[i]); }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:70:44: warning: narrowing conversion of 's' from 'long long int' to 'double' inside { } [-Wnarrowing]
                 Lines[L] = {Sum[n], c, s, n};
                                            ^
sequence.cpp:52:15: warning: unused variable 'r' [-Wunused-variable]
     long long r, s, c;
               ^
sequence.cpp:43: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:47: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...