結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー wunderkammer2wunderkammer2
提出日時 2020-10-09 23:59:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 3,662 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 133,176 KB
実行使用メモリ 11,180 KB
最終ジャッジ日時 2023-09-27 20:16:57
合計ジャッジ時間 14,673 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 69 ms
10,780 KB
testcase_19 AC 64 ms
10,220 KB
testcase_20 AC 38 ms
7,392 KB
testcase_21 AC 10 ms
4,376 KB
testcase_22 AC 42 ms
8,036 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 55 ms
9,164 KB
testcase_25 AC 40 ms
7,472 KB
testcase_26 AC 18 ms
4,684 KB
testcase_27 AC 10 ms
4,380 KB
testcase_28 AC 68 ms
10,840 KB
testcase_29 AC 70 ms
11,012 KB
testcase_30 AC 70 ms
11,180 KB
testcase_31 AC 73 ms
11,052 KB
testcase_32 AC 70 ms
11,008 KB
testcase_33 AC 71 ms
10,920 KB
testcase_34 AC 69 ms
10,844 KB
testcase_35 AC 73 ms
11,128 KB
testcase_36 AC 68 ms
10,844 KB
testcase_37 AC 74 ms
11,012 KB
testcase_38 AC 70 ms
10,844 KB
testcase_39 AC 67 ms
10,948 KB
testcase_40 AC 68 ms
11,012 KB
testcase_41 AC 69 ms
10,984 KB
testcase_42 AC 73 ms
10,936 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>     //sort,二分探索,など
#include<bit>           //popcount
#include<bitset>        //固定長bit集合
#include<cmath>         //pow,logなど
#include<complex>       //複素数
#include<deque>         //両端アクセスのキュー
#include<fstream>       //ファイルストリーム(標準入力変更用)
#include<functional>    //sortのgreater
#include<iomanip>       //setprecision(浮動小数点の出力の誤差)
#include<iostream>      //入出力
#include<iterator>      //集合演算(積集合,和集合,差集合など)
#include<map>           //map(辞書)
#include<numeric>       //iota(整数列の生成),gcdとlcm(c++17)
#include<queue>         //キュー
#include<set>           //集合
#include<stack>         //スタック
#include<string>        //文字列
#include<unordered_map> //イテレータあるけど順序保持しないmap
#include<unordered_set> //イテレータあるけど順序保持しないset
#include<utility>       //pair
#include<vector>        //可変長配列

//名前
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef map<string, int> msi;
typedef map<string, ll> msll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pllll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<string>> vvs;
typedef vector<vector<bool>> vvb;

//定数
const ll MOD = 1000000007;
const ll INF = 1000000000000000000;
const int MAXR = 100000;             //10^5:配列の最大のrange

//マクロ
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,s,e) for(int i=s;i<e;i++)
#define repse(i,s,e) for(int i=s;i<=e;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define in1(x1) cin >> x1
#define in2(x1, x2) cin >> x1 >> x2
#define in3(x1, x2, x3) cin >> x1 >> x2 >> x3
#define inN(x, N) rep(i, N) in1(x[i])
#define outl(x) cout << x << endl
#define out2l(x, y) cout << x << " " << y << endl 

//よく使う関数
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    //標準入力をファイルに変更
    //std::ifstream in("input.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N;
    in1(N);

    vector<pllll> v(N);

    rep(i, N) in1(v[i].first);
    rep(i, N) in1(v[i].second);

    sort(all(v));

    vll X, Y;
    X.push_back(v.front().first);
    Y.push_back(v.front().second);

    reps(i, 1, N)
    {
        if (X.back() == v[i].first)
        {
            Y.back() += v[i].second;
        }
        else
        {
            X.push_back(v[i].first);
            Y.push_back(v[i].second);
        }
    }

    vll YSum(Y.size() + 1);
    rep(i, Y.size()) YSum[i + 1] = YSum[i] + Y[i];

    const int MIN_A = -1000000;
    const int MAX_A = 1000000;

    ll tmp = 0;
    rep(i, Y.size())
    {
        tmp += Y[i] * abs(X[i] - X.front());
    }

    int x = 0;
    ll ans = tmp;

    int idx = 0;

    repse(i, X.front() + 1, X.back())
    {
        if (i <= X.back() && i > X[idx] && idx < Y.size()) idx++;

        if (i > X[0])
        {
            tmp += (YSum[idx] - YSum[0]);
        }
        
        if (i <= X.back())
        {
            tmp -= (YSum.back() - YSum[idx]);
        }

        if (chmin(ans, tmp)) x = i;
    }

    out2l(x, ans);

    return 0;
}
0