結果
| 問題 | No.1251 絶対に間違ってはいけない最小化問題 | 
| コンテスト | |
| ユーザー |  Forested | 
| 提出日時 | 2020-10-09 22:06:13 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 93 ms / 2,000 ms | 
| コード長 | 1,951 bytes | 
| コンパイル時間 | 2,100 ms | 
| コンパイル使用メモリ | 203,856 KB | 
| 最終ジャッジ日時 | 2025-01-15 04:46:20 | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 42 | 
ソースコード
// Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T> 
inline bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
struct IOSET {
    IOSET() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} ioset;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &element : vec) is >> element;
    return is;
}
template <typename T>
vector<T> operator--(vector<T> &vec) {
    for (T &element : vec) --element;
    return vec;
}
// Main
int main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    cin >> a >> b;
    
    vector<ll> v;
    rep(i, n) v.push_back(a[i]);
    sort(all(v));
    v.erase(unique(all(v)), v.end());
    vector<int> a_(n);
    rep(i, n) a_[i] = lower_bound(all(v), a[i]) - v.begin();
    
    vector<ll> x(v.size(), 0);
    rep(i, n) {
        x[0] -= b[i];
        x[a_[i]] += b[i];
        x[a_[i]] += b[i];
        x[v.size() - 1] -= b[i];
    } 
    rep(i, v.size() - 1) x[i + 1] += x[i];
    vector<ll> f(v.size());
    ll res = 0;
    rep(i, n) res += b[i] * abs(a[i] - v[0]);
    f[0] = res;
    rep(i, v.size() - 1) f[i + 1] = f[i] + x[i] * (v[i + 1] - v[i]);
    ll mn = INF, idx = 0;
    rep(i, v.size()) if (chmin(mn, f[i])) idx = i;
    cout << v[idx] << " " << mn << "\n";
}
            
            
            
        