제출 #59730

#제출 시각아이디문제언어결과실행 시간메모리
59730model_codeHomecoming (BOI18_homecoming)C++17
31 / 100
121 ms8636 KiB
// Andrei-Costin Constantinescu // O(N^2) - Dynamic Programming #include "homecoming.h" #include <bits/stdc++.h> using namespace std; void subtaskAsserts(int N, int K, int a[], int b[]) { assert(N <= 5000); } typedef long long int lint; const int NMAX = 2000000; const int VALMAX = 1000000000; void asserts(int N, int K, int a[], int b[]) { assert(1 <= K && K <= N && N <= NMAX); for (int i = 0; i < N; ++ i) { assert(0 <= a[i] && a[i] <= VALMAX); assert(0 <= b[i] && b[i] <= VALMAX); } } int A[2 * NMAX + 5], B[2 * NMAX + 5]; lint dpTaken[2 * NMAX + 5]; lint dp[2 * NMAX + 5]; lint sPartB[2 * NMAX]; inline lint getSPartB(int l, int r) { lint sum = sPartB[r]; if (l > 0) sum -= sPartB[l - 1]; return sum; } lint solve(int N, int K, int a[], int b[]) { asserts(N, K, a, b), subtaskAsserts(N, K, a, b); // Copy and double a, b for internal use for (int i = 0; i < N; ++ i) { A[i] = a[i], A[N + i] = a[i]; B[i] = b[i], B[N + i] = b[i]; } // Compute the partial sums of B sPartB[0] = B[0]; for (int i = 1; i < 2 * N; ++ i) sPartB[i] = sPartB[i - 1] + B[i]; // Try to take all textbooks lint ans = 0; for (int i = 0; i < N; ++ i) ans += A[i] - B[i]; // Fix a textbook we'll NOT take for (int i = 0; i < N; ++ i) { dp[i + N] = 0; for (int j = i + N - 1; j >= i + 1; -- j) { if (j + K - 1 < i + N) { dpTaken[j] = dp[j + K]; if (j + K < i + N) dpTaken[j] = max(dpTaken[j], dpTaken[j + 1] + A[j + 1] - B[j + K]); } dp[j] = dp[j + 1]; if (j + K - 1 < i + N) dp[j] = max(dp[j], dpTaken[j] + A[j] - getSPartB(j, j + K - 1)); } ans = max(ans, dp[i + 1]); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...