結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー yuji9511yuji9511
提出日時 2020-10-09 22:51:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,648 bytes
コンパイル時間 2,048 ms
コンパイル使用メモリ 206,272 KB
実行使用メモリ 12,112 KB
最終ジャッジ日時 2023-09-27 18:51:27
合計ジャッジ時間 15,836 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,400 KB
testcase_01 AC 2 ms
4,992 KB
testcase_02 AC 2 ms
5,460 KB
testcase_03 AC 2 ms
5,188 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
5,064 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,992 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
5,064 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,992 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
5,368 KB
testcase_17 AC 1 ms
5,064 KB
testcase_18 AC 63 ms
10,360 KB
testcase_19 AC 61 ms
10,904 KB
testcase_20 AC 35 ms
7,420 KB
testcase_21 AC 7 ms
5,644 KB
testcase_22 AC 38 ms
6,944 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 51 ms
9,716 KB
testcase_25 AC 37 ms
6,780 KB
testcase_26 AC 15 ms
4,716 KB
testcase_27 AC 6 ms
5,864 KB
testcase_28 AC 67 ms
11,488 KB
testcase_29 AC 65 ms
11,084 KB
testcase_30 AC 66 ms
12,112 KB
testcase_31 AC 67 ms
11,064 KB
testcase_32 AC 66 ms
11,100 KB
testcase_33 AC 66 ms
11,084 KB
testcase_34 AC 66 ms
11,232 KB
testcase_35 AC 65 ms
11,608 KB
testcase_36 AC 67 ms
10,592 KB
testcase_37 AC 67 ms
10,908 KB
testcase_38 AC 65 ms
11,072 KB
testcase_39 AC 66 ms
10,864 KB
testcase_40 AC 66 ms
10,748 KB
testcase_41 AC 65 ms
11,256 KB
testcase_42 AC 67 ms
11,052 KB
testcase_43 AC 2 ms
5,420 KB
testcase_44 AC 2 ms
5,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << "\n"

void solve(){
    ll N;
    cin >> N;
    ll A[200010], B[200010];
    rep(i,0,N) cin >> A[i];
    rep(i,0,N) cin >> B[i];
    vector<lpair> lp;
    rep(i,0,N) lp.push_back({A[i], B[i]});
    sort(lp.begin(), lp.end());
    rep(i,0,N){
        A[i] = lp[i].first;
        B[i] = lp[i].second;
    }
    ll plus_val = 0, minus_val = 0;
    rep(i,1,N) plus_val += B[i];
    ll ans = 0;
    rep(i,0,N){
        ans += B[i] * abs(A[i] - A[0]);
    }
    ll cur = ans, min_pos = A[0];
    // print(cur);
    rep(i,1,N){
        cur -= plus_val * (A[i] - A[i-1]);
        minus_val += B[i-1];
        cur += minus_val * (A[i] - A[i-1]);
        if(cur < ans){
            ans = cur;
            min_pos = A[i];
        }
        ans = min(ans, cur);
        plus_val -= B[i];
    }
    print(min_pos,ans);

    // ll ans2 = INF;
    // rep(i,0,N){
    //     ll r = 0;
    //     rep(j,0,N) r += B[j] * abs(A[j] - A[i]);
    //     ans2 = min(ans2, r);
    // }
    // print(ans2);


}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
0