結果
問題 | No.913 木の燃やし方 |
ユーザー | square1001 |
提出日時 | 2019-10-18 23:12:24 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,416 bytes |
コンパイル時間 | 1,628 ms |
コンパイル使用メモリ | 121,616 KB |
実行使用メモリ | 49,076 KB |
最終ジャッジ日時 | 2024-06-25 19:15:07 |
合計ジャッジ時間 | 19,326 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 5 ms
5,376 KB |
testcase_06 | WA | - |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 361 ms
38,324 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | AC | 359 ms
39,260 KB |
testcase_13 | AC | 235 ms
35,628 KB |
testcase_14 | AC | 226 ms
34,484 KB |
testcase_15 | AC | 223 ms
34,328 KB |
testcase_16 | AC | 940 ms
47,672 KB |
testcase_17 | AC | 894 ms
46,292 KB |
testcase_18 | AC | 859 ms
46,264 KB |
testcase_19 | AC | 368 ms
47,628 KB |
testcase_20 | AC | 901 ms
49,040 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 632 ms
47,588 KB |
testcase_25 | AC | 361 ms
47,608 KB |
testcase_26 | AC | 240 ms
36,680 KB |
testcase_27 | AC | 238 ms
37,576 KB |
testcase_28 | AC | 242 ms
36,920 KB |
testcase_29 | AC | 252 ms
36,632 KB |
testcase_30 | AC | 345 ms
45,924 KB |
testcase_31 | AC | 255 ms
36,728 KB |
testcase_32 | AC | 237 ms
36,664 KB |
testcase_33 | AC | 235 ms
36,844 KB |
testcase_34 | AC | 456 ms
40,964 KB |
testcase_35 | AC | 479 ms
41,212 KB |
testcase_36 | AC | 869 ms
47,776 KB |
ソースコード
#include <set> #include <deque> #include <vector> #include <iostream> #include <algorithm> using namespace std; vector<long long> subsolve(int N, vector<long long> A) { A.insert(A.begin(), 0); for (int i = 0; i < N; ++i) { A[i + 1] += A[i]; } // minimize (A[i] - A[j]) + (i - j)^2 = (A[i] + i * i) + (-A[j] + j * j) - 2 * j * i for each i vector<long long> intercept(N + 1), slope(N + 1); for (int i = 0; i <= N; ++i) { intercept[i] = -A[i] + 1LL * i * i; slope[i] = -2 * i; } vector<int> seq(N + 1); deque<int> que; for (int i = 0; i <= N; ++i) { if (i >= 1) { seq[i] = que[0]; } while (que.size() >= 2) { int pa = que[que.size() - 2]; int pb = que[que.size() - 1]; double cra = -double(intercept[pa] - intercept[i]) / (slope[pa] - slope[i]); double crb = -double(intercept[pb] - intercept[i]) / (slope[pb] - slope[i]); if (cra > crb) que.pop_back(); else break; } que.push_back(i); while (que.size() >= 2) { int pa = que[0]; int pb = que[1]; long long va = 1LL * slope[pa] * (i + 1) + intercept[pa]; long long vb = 1LL * slope[pb] * (i + 1) + intercept[pb]; if (va > vb) que.pop_front(); else break; } } vector<vector<long long> > qs; for (int i = 1; i <= N; ++i) { for (int j = seq[i - 1]; j <= seq[i]; ++j) { if (j != i) { qs.push_back({ A[i] - A[j] + 1LL * (i - j) * (i - j), j, i }); } } } sort(qs.begin(), qs.end()); set<int> st; for (int i = 0; i < N; ++i) { st.insert(i); } vector<long long> ans(N); for (vector<long long> i : qs) { set<int>::iterator it = st.lower_bound(i[1]); while (it != st.end() && *it < i[2]) { ans[*it] = i[0]; it = st.erase(it); } } return ans; } vector<long long> solve(int N, vector<long long> A) { vector<long long> res1 = subsolve(N, A); reverse(A.begin(), A.end()); vector<long long> res2 = subsolve(N, A); reverse(res2.begin(), res2.end()); for (int i = 0; i < N; ++i) { res1[i] = min(res1[i], res2[i]); } return res1; } vector<long long> solve_easy(int N, vector<long long> A) { vector<long long> ans(N, 1LL << 62); for (int i = 0; i < N; ++i) { for (int j = i + 1; j <= N; ++j) { long long sum = 1LL * (j - i) * (j - i); for (int k = i; k < j; ++k) { sum += A[k]; } for (int k = i; k < j; ++k) { ans[k] = min(ans[k], sum); } } } return ans; } #include <random> #include <string> using namespace std; mt19937 mt(1910182301); int rand_rng(int l, int r) { uniform_int_distribution<int> p(l, r - 1); return p(mt); } string stringify(vector<long long> A) { string res = "["; for (int i = 0; i < A.size(); ++i) { if (i) res += ", "; res += to_string(A[i]); } res += "]"; return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; ++i) cin >> A[i]; vector<long long> ans = solve(N, A); for (int i = 0; i < N; ++i) { cout << ans[i] << '\n'; } /* int N = 3; for (int i = 1; i <= 10000; ++i) { vector<long long> A(N); for (int j = 0; j < N; ++j) { A[j] = rand_rng(-6, 7); } vector<long long> res1 = solve(N, A); vector<long long> res2 = solve_easy(N, A); if (res1 != res2) { cout << "Case #" << i << ":" << endl; cout << "N = " << N << ", A = " << stringify(A) << endl; cout << "Returns: " << stringify(res1) << endl; cout << "Answer: " << stringify(res2) << endl; cout << endl; } } */ return 0; }