結果
| 問題 | No.2169 To Arithmetic |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-08-21 18:25:40 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 137 ms / 2,000 ms |
| コード長 | 2,527 bytes |
| コンパイル時間 | 2,219 ms |
| コンパイル使用メモリ | 204,104 KB |
| 最終ジャッジ日時 | 2025-01-31 02:32:05 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
// clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using V = vector<T>;
using VI = V<int>;
using VL = V<ll>;
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(n) for(ll i=0;i<(n);++i)
#define rep2(i,n) for(ll i=0;i<(n);++i)
#define rep3(i,a,b) for(ll i=(a);i<(b);++i)
#define rep4(i,a,b,c) for(ll i=(a);i<(b);i+=(c))
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define all(a) a.begin(),a.end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 61;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T> inline istream& operator>>(istream& is, V<T>& v) { for (auto& a : v)is >> a; return is; }
// clang-format on
using pll = pair<ll, ll>;
ll cross(pll a, pll b) { return a.first * b.second - a.second * b.first; }
int ccw(pll a, pll b, pll c) {
b.first -= a.first;
b.second -= a.second;
c.first -= a.first;
c.second -= a.second;
if (cross(b, c) == 0) return 0;
return cross(b, c) > 0 ? 1 : -1;
}
int main() {
init();
int N, Q;
cin >> N >> Q;
VL A(N);
cin >> A;
// Bをソートして累積和を用意する
VL B(N - 1);
rep(i, N - 1) B[i] = A[i + 1] - A[i];
sort(all(B));
VL C(N);
rep(i, N - 1) C[i + 1] = C[i] + B[i];
// 上側凸包を求める
V<pll> P;
int j = 0;
rep(i, N) {
while (2 <= j && ccw(P[j - 2], P[j - 1], pll(i, A[i])) >= 0) {
--j;
P.pop_back();
}
P.emplace_back(i, A[i]);
++j;
}
V<pll> D; // 傾きdy/dxを(dx,dy)のペアとして保持する
rep(i, P.size() - 1) {
ll dx = P[i + 1].first - P[i].first;
ll dy = P[i + 1].second - P[i].second;
D.emplace_back(dx, dy);
}
D.emplace_back(1, -INF);
while (Q--) {
ll d;
cin >> d;
int ng = -1, ok = (int)D.size() - 1;
while (ok - ng > 1) {
int mid = ok + ng >> 1;
if (D[mid].second <= d * D[mid].first) ok = mid;
// D[mid].second / D[mid].first <= d
else ng = mid;
}
int x = P[ok].first;
ll A0 = A[x] - d * (x + 1);
ll B0 = A[0] - A0;
int up = upper_bound(all(B), d) - B.begin();
cout << (up * d - C[up]) + (d - B0) << "\n";
}
return 0;
}