結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー snow39snow39
提出日時 2020-10-09 22:24:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,224 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 98,796 KB
実行使用メモリ 7,876 KB
最終ジャッジ日時 2023-09-27 18:05:07
合計ジャッジ時間 13,640 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 65 ms
7,620 KB
testcase_19 AC 63 ms
7,220 KB
testcase_20 AC 35 ms
5,376 KB
testcase_21 AC 7 ms
4,380 KB
testcase_22 AC 38 ms
5,644 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 53 ms
6,380 KB
testcase_25 AC 38 ms
5,684 KB
testcase_26 AC 14 ms
4,376 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 69 ms
7,816 KB
testcase_29 AC 66 ms
7,836 KB
testcase_30 AC 68 ms
7,792 KB
testcase_31 AC 68 ms
7,812 KB
testcase_32 AC 72 ms
7,864 KB
testcase_33 AC 66 ms
7,804 KB
testcase_34 AC 69 ms
7,868 KB
testcase_35 AC 67 ms
7,752 KB
testcase_36 AC 67 ms
7,780 KB
testcase_37 AC 67 ms
7,812 KB
testcase_38 AC 66 ms
7,740 KB
testcase_39 AC 67 ms
7,876 KB
testcase_40 AC 66 ms
7,804 KB
testcase_41 AC 68 ms
7,860 KB
testcase_42 AC 67 ms
7,872 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

const ll INF=1e18;

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

  int N;
  cin>>N;
  vector<ll> A(N),B(N);
  rep(i,N) cin>>A[i];
  rep(i,N) cin>>B[i];

  vector<ll> X=A;
  sort(all(X));
  X.erase(unique(all(X)),X.end());
  int V=X.size();

  auto calc = [&](ll x){
    ll res=0;
    rep(i,N) res+=B[i]*abs(x-A[i]);
    return res;
  };

  int low=0,high=V-1;
  while(low+2<high){
    int lef=(low+low+high)/3;
    int rig=(low+high+high)/3;

    ll lv=calc(X[lef]);
    ll rv=calc(X[rig]);
    if(lv<rv) high=rig;
    else low=lef; 
  }

  ll ans=INF;
  int mit=0;
  while(low<=high){
    ll res=calc(X[low]);
    if(ans>res){
      ans=res;
      mit=low;
    }
    low++;
  }
  cout<<X[mit]<<" "<<ans<<endl;

  return 0;
}
0