結果

問題 No.860 買い物
ユーザー heno239heno239
提出日時 2019-08-09 22:01:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 1,000 ms
コード長 2,690 bytes
コンパイル時間 1,869 ms
コンパイル使用メモリ 116,116 KB
実行使用メモリ 5,492 KB
最終ジャッジ日時 2023-09-26 17:53:45
合計ジャッジ時間 2,760 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 30 ms
5,356 KB
testcase_08 AC 30 ms
5,424 KB
testcase_09 AC 29 ms
5,476 KB
testcase_10 AC 30 ms
5,492 KB
testcase_11 AC 23 ms
5,416 KB
testcase_12 AC 23 ms
5,368 KB
testcase_13 AC 29 ms
5,408 KB
testcase_14 AC 31 ms
5,356 KB
testcase_15 AC 17 ms
5,332 KB
testcase_16 AC 26 ms
5,352 KB
testcase_17 AC 30 ms
5,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<int> vec;
typedef vector<string> svec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;

struct SegT {
private:
	int sz; vector<ll> node;
	const ll init_c = INF;
public:
	SegT(int n) {
		sz = 1;
		while (sz < n)sz *= 2;
		node.resize(2 * sz - 1, init_c);
	}
	ll f(ll a, ll b) {
		return min(a, b);
	}
	void update(int k, ll a) {
		k += sz - 1;
		node[k] = a;
		while (k > 0) {
			k = (k - 1) / 2;
			node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
		}
	}
	ll query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0)r = sz;
		if (r <= a || b <= l)return init_c;
		else if (a <= l && r <= b)return node[k];
		else {
			ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
			ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
			return f(vl, vr);
		}
	}
};

void solve() {
	int n; cin >> n;
	vector<ll> c(n), d(n);
	vector<ll> r(n + 1);
	rep(i, n) {
		cin >> c[i] >> d[i];
		r[i + 1] = r[i] + c[i] + d[i];
	}
	//cout << r[n] << endl;
	deque<LP> q;
	q.push_back({ -d[0],INF });
	rep1(i, n) {
		while (q.size() >= 2) {
			LP z2 = q.back(); q.pop_back();
			LP z1 = q.back(); q.pop_back();
			if (z1.second >= c[i - 1]) {
				z1.second = c[i - 1];
			}
			if (z2.second >= c[i - 1]) {
				z2.second = c[i - 1];
			}
			if (z1.first + z1.second >= z2.first + z2.second) {
				q.push_back(z2);
			}
			else if (z1.second == z2.second) {
				q.push_back(z1);
			}
			else {
				q.push_back(z1); q.push_back(z2); break;
			}
		}
		if (!q.empty()) {
			LP &z = q.back();
			if (z.second >= c[i - 1])z.second = c[i - 1];
		}
		ll ans = q.front().first + q.front().second;
		if (i == n) {
			cout << ans+r[n] << endl; break;
		}
		q.push_back({ ans-d[i],INF });
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
	//stop
	return 0;
}
0