제출 #875979

#제출 시각아이디문제언어결과실행 시간메모리
875979rukashii수열 (APIO14_sequence)C++17
39 / 100
2064 ms2980 KiB
#include <bits/stdc++.h> using namespace std; #define file if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } #define int long long void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void setIO(string s = "") { if (s.size()) setIn(s+".in"), setOut(s+".out"); } const int maxn = 1e5 + 2, maxk = 202; int a[maxn], prf[maxn], n, k; namespace Sub2 { const int s2maxn = 52, s2maxk = 52; int dp[s2maxn][s2maxn][s2maxk]; int cal(int l, int r, int krem) { if (l == r || krem == 0) return dp[l][r][krem] = 0; int res = dp[l][r][krem]; if (res != -1) return res; res = 0; for (int i = l; i < r; i++) { for (int lk = 0; lk < krem; lk++) { int rk = krem - lk - 1; res = max(res, cal(l, i, lk) + cal(i + 1, r, rk) + (prf[r] - prf[i]) * (prf[i] - prf[l - 1])); } } return dp[l][r][krem] = res; } void trace(int l, int r, int krem) { bool skip = 0; for (int i = l; i < r; i++) { for (int lk = 0; lk < krem; lk++) { int rk = krem - lk - 1; if (dp[l][r][krem] == cal(l, i, lk) + cal(i + 1, r, rk) + (prf[r] - prf[i]) * (prf[i] - prf[l - 1])) { cout << i << ' '; trace(1, i, lk); trace(i + 1, r, rk); skip = 1; break; } } if (skip) break; } } void solve() { memset(dp, -1, sizeof(dp)); cout << cal(1, n, k) << '\n'; trace(1, n, k); } } namespace Sub4 { const int s4maxn = 1002, s4maxk = 202; int dp[s4maxn][s4maxk]; void solve() { // dp[0][0] = 0; k++; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { for (int p = 0; p < i; p++) { //from n -> i + 1 //from i -> p + 1 dp[i][j] = max(dp[p][j - 1] + (prf[n] - prf[i]) * (prf[i] - prf[p]), dp[i][j]); } } } cout << dp[n][k] << '\n'; int curk = k, curb = n; while (curk > 0) { for (int i = 0; i < curb; i++) { if (dp[i][curk - 1] + (prf[n] - prf[curb]) * (prf[curb] - prf[i]) == dp[curb][curk]) { if (i) cout << i << ' '; curk--; curb = i; } } } } } signed main() { // setIO(); file; ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], prf[i] = prf[i - 1] + a[i]; if (n <= 50) return Sub2::solve(), 0; if (n <= 1000) return Sub4::solve(), 0; }

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'void setIn(std::string)':
sequence.cpp:7:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'void setOut(std::string)':
sequence.cpp:8:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:4:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    4 | #define file  if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
      |                                              ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:118:5: note: in expansion of macro 'file'
  118 |     file;
      |     ^~~~
sequence.cpp:4:87: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    4 | #define file  if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
      |                                                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:118:5: note: in expansion of macro 'file'
  118 |     file;
      |     ^~~~
#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...