結果

問題 No.1012 荷物収集
ユーザー rxoxixyxdrxoxixyxd
提出日時 2020-03-20 23:07:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,832 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 179,648 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 18:38:18
合計ジャッジ時間 6,798 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

typedef long long ll;

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; }
const int INF = 1e9;
const long long INFL = 1LL<<60;


int main()
{
  int n, q;
  cin >> n >> q;

  vector<ll> x(n);
  vector<ll> w(n);
  rep(i, n) {
    cin >> x[i] >> w[i];
  }

  ll sum_w = SUM(w);

  map<ll, ll> mp_l;
  map<ll, ll> mp_r;
  map<ll, ll> mp_lw;
  map<ll, ll> mp_rw;
  for (int i = 0; i < n; i++) {
    ll cost = 0;
    ll weight = 0;
    for (int j = 0; j <= i; j++) {
      cost += abs(x[i] - x[j]) * w[j];
      weight += w[j];
    }
    mp_l[x[i]] = cost;
    mp_lw[x[i]] = weight;
  }
  for (int i = n-1; i >= 0; i--) {
    ll cost = 0;
    ll weight = 0;
    for (int j = n-1; j >= i; j--) {
      cost += abs(x[i] - x[j]) * w[j];
      weight += w[j];
    }
    mp_r[x[i]] = cost;
    mp_rw[x[i]] = weight;
  }


  sort(ALL(x));

  rep(i, q) {
    ll target;
    cin >> target;
    // 1番近い xi を求める
    auto itr1 = lower_bound(ALL(x), target);
    if (itr1 != x.begin() || itr1 == x.end()) itr1--;
    auto itr2 = upper_bound(ALL(x), target);
    if (itr2 == x.end()) itr2--;
    ll ans = 0;
    if (*itr1 < target) {
      ans += mp_l[*itr1] + abs(*itr1 - target) * mp_lw[*itr1];
    }
    if (*itr2 > target) {
      ans += mp_r[*itr2] + abs(*itr2 - target) * mp_rw[*itr2];
    }
    cout << ans << endl;
  }

  return 0;
}
0