結果
問題 | No.1251 絶対に間違ってはいけない最小化問題 |
ユーザー |
|
提出日時 | 2020-10-09 22:18:11 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 132 ms / 2,000 ms |
コード長 | 1,289 bytes |
コンパイル時間 | 1,139 ms |
コンパイル使用メモリ | 101,620 KB |
最終ジャッジ日時 | 2025-01-15 04:57:38 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#include <iostream>#include <iomanip>#include <algorithm>#include <vector>#include <map>using lint = long long;template <class T>std::map<T, int> compress(std::vector<T>& v) {std::sort(v.begin(), v.end());v.erase(std::unique(v.begin(), v.end()), v.end());std::map<T, int> rev;for (int i = 0; i < (int)v.size(); ++i) rev[v[i]] = i;return rev;}constexpr lint XINF = 1 << 20;void solve() {int n;std::cin >> n;std::vector<lint> xs(n), ys(n);for (auto& x : xs) std::cin >> x;for (auto& y : ys) std::cin >> y;auto eval = [&](lint x) {lint ret = 0;for (int i = 0; i < n; ++i) {ret += std::abs(xs[i] - x) * ys[i];}return ret;};auto zs = xs;zs.push_back(-XINF);zs.push_back(XINF);compress(zs);int m = zs.size();int ok = -1, ng = m;while (ng - ok > 1) {int mid = (ok + ng) / 2;if (eval(zs[mid] - 1) > eval(zs[mid])) {ok = mid;} else {ng = mid;}}std::cout << zs[ok] << " " << eval(zs[ok]) << "\n";}int main() {std::cin.tie(nullptr);std::ios::sync_with_stdio(false);std::cout << std::fixed << std::setprecision(10);solve();return 0;}