結果
問題 | No.1391 ±1 Abs Sum |
ユーザー | norikame |
提出日時 | 2021-02-12 23:11:10 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,168 bytes |
コンパイル時間 | 2,316 ms |
コンパイル使用メモリ | 204,032 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-07-20 00:42:25 |
合計ジャッジ時間 | 5,561 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | WA | - |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | AC | 51 ms
7,552 KB |
testcase_15 | AC | 56 ms
7,808 KB |
testcase_16 | AC | 68 ms
9,088 KB |
testcase_17 | AC | 60 ms
8,320 KB |
testcase_18 | AC | 82 ms
10,240 KB |
testcase_19 | AC | 61 ms
8,320 KB |
testcase_20 | AC | 88 ms
10,752 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | AC | 92 ms
11,008 KB |
testcase_32 | AC | 48 ms
7,168 KB |
testcase_33 | AC | 53 ms
7,808 KB |
testcase_34 | AC | 88 ms
10,624 KB |
testcase_35 | AC | 2 ms
5,376 KB |
testcase_36 | WA | - |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define v(t) vector<t> #define p(t) pair<t, t> #define p2(t, s) pair<t, s> #define vp(t) v(p(t)) #define rep(i, n) for (int i=0,i##_len=((int)(n)); i<i##_len; ++i) #define rep2(i, a, n) for (int i=((int)(a)),i##_len=((int)(n)); i<=i##_len; ++i) #define repr(i, n) for (int i=((int)(n)-1); i>=0; --i) #define rep2r(i, a, n) for (int i=((int)(n)),i##_len=((int)(a)); i>=i##_len; --i) #define repi(itr, c) for (__typeof((c).begin()) itr=(c).begin(); itr!=(c).end(); ++itr) #define repir(itr, c) for (__typeof((c).rbegin()) itr=(c).rbegin(); itr!=(c).rend(); ++itr) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define SORT(v, n) sort(v, v+n); #define VSORT(v) sort(v.begin(), v.end()); #define RSORT(x) sort(rall(x)); #define pb push_back #define eb emplace_back #define INF (1e9) #define LINF (1e18) #define PI (acos(-1)) #define EPS (1e-7) #define DEPS (1e-10) int main(){ int n, k; cin >> n >> k; v(ll) a(n); rep(i, n) cin >> a[i]; /* if (k == n) { ll ans = (ll)(LINF); if (n%2==0) { ll sum1 = 0, sum2 = 0, cval1 = 0, cval2 = 0; cval1 = (a[n/2-1]+a[n/2]) / 2, cval2 = cval1 + 1; rep(i, n) sum1 += llabs(cval1 - a[i]); rep(i, n) sum2 += llabs(cval2 - a[i]); ans = min(ans, min(sum1, sum2)); } else { ll cval = a[n/2], sum = 0; rep(i, n) sum += llabs(cval-a[i]); ans = min(ans, sum); } cout << ans << endl; } */ { v(ll) al(n), ar(n); rep(i, n) al[i] = a[i] - a[0]; repr(i, n) ar[i] = llabs(a[i] - a[n-1]); reverse(all(ar)); v(ll) suml(n+1), sumr(n+1); rep(i, n) suml[i+1] = suml[i] + al[i]; rep(i, n) sumr[i+1] = sumr[i] + ar[i]; ll pvall = suml[n] - suml[k], pvalr = sumr[n] - sumr[k]; if (k == 0) { cout << (-1)*max(suml[n],sumr[n]) << endl; return 0; } ll mval = (ll)(LINF); rep2(i, 1, k) { ll ttar = al[i-1]; ll nval = ((i-1)*ttar - suml[i-1]) + ((suml[k]-suml[i]) - (k-i)*ttar) - (pvall - (n-k)*ttar); mval = min(mval, nval); if (i < k) { ttar += (al[i] - al[i-1]) / 2; nval = ((i-1)*ttar - suml[i-1]) + ((suml[k]-suml[i]) - (k-i)*ttar) - (pvall - (n-k)*ttar); mval = min(mval, nval); } } rep2(i, 1, k) { ll ttar = ar[i-1]; ll nval = ((i-1)*ttar - sumr[i-1]) + ((sumr[k]-sumr[i]) - (k-i)*ttar) - (pvalr - (n-k)*ttar); mval = min(mval, nval); if (i < k) { ttar += (ar[i] - ar[i-1]) / 2; nval = ((i-1)*ttar - sumr[i-1]) + ((sumr[k]-sumr[i]) - (k-i)*ttar) - (pvalr - (n-k)*ttar); mval = min(mval, nval); } } cout << mval << endl; } return 0; }