結果
| 問題 |
No.704 ゴミ拾い Medium
|
| コンテスト | |
| ユーザー |
ei1333333
|
| 提出日時 | 2018-06-15 23:13:29 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,191 bytes |
| コンパイル時間 | 4,404 ms |
| コンパイル使用メモリ | 199,560 KB |
| 最終ジャッジ日時 | 2025-01-05 14:08:39 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 4 |
| other | WA * 44 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:61:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
61 | scanf("%lld", &N);
| ~~~~~^~~~~~~~~~~~
main.cpp:62:35: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
62 | for(int i = 0; i < N; i++) scanf("%lld", &A[i]);
| ~~~~~^~~~~~~~~~~~~~~
main.cpp:63:35: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
63 | for(int i = 0; i < N; i++) scanf("%lld", &X[i]);
| ~~~~~^~~~~~~~~~~~~~~
main.cpp:64:35: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
64 | for(int i = 0; i < N; i++) scanf("%lld", &Y[i]);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int64 INF = 1LL << 58;
template< typename Monoid >
struct SegmentTree {
using F = function< Monoid(Monoid, Monoid) >;
int sz;
vector< Monoid > seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while(sz < n) sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) {
seg[k + sz] = x;
}
void build() {
for(int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = max(seg[k], x);
while(k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if(a & 1) L = f(L, seg[a++]);
if(b & 1) R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const {
return seg[k + sz];
}
};
int main() {
int64 N, A[300001];
int64 X[300001], Y[300001];
scanf("%lld", &N);
for(int i = 0; i < N; i++) scanf("%lld", &A[i]);
for(int i = 0; i < N; i++) scanf("%lld", &X[i]);
for(int i = 0; i < N; i++) scanf("%lld", &Y[i]);
/*
vector< int64 > dp(N + 1, INF);
dp[0] = 0;
for(int i = 1; i <= N; i++) {
int64 ret = INF;
for(int j = i-1; j >= 0; j--) {
int64 dist = dp[j] + abs(A[i - 1] - X[j]) + Y[j];
ret = min(ret, dist);
}
dp[i] = ret;
}
cout << dp[N] << endl;
*/
auto f = [&](int64 x, int64 y) { return min(x, y); };
SegmentTree< int64 > latte(N + 1, f, INF), malta(N + 1, f, INF);
auto update = [&](int idx, int64 v) {
auto it = lower_bound(A, A + N, X[idx]) - A;
latte.update(it, v + Y[idx] + X[idx]);
malta.update(it, v + Y[idx] - X[idx]);
};
update(0, 0);
for(int i = 1; i <= N; i++) {
int64 ret = min(latte.query(i - 1, N + 1) - A[i - 1], malta.query(0, i - 1) + A[i - 1]);
// dp[i] = (dp[j]+Y[j]+X[j])-A[i-1] (A[i-1]≦X[j])
// (dp[j]+Y[j]-X[j])+A[i-1] (A[i-1]≧X[j])
if(i == N) cout << ret << endl;
else update(i, ret);
}
}
ei1333333