結果

問題 No.860 買い物
ユーザー chocoruskchocorusk
提出日時 2019-08-09 23:07:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 108 ms / 1,000 ms
コード長 1,773 bytes
コンパイル時間 1,936 ms
コンパイル使用メモリ 98,472 KB
実行使用メモリ 11,844 KB
最終ジャッジ日時 2023-09-26 21:47:54
合計ジャッジ時間 3,537 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,500 KB
testcase_01 AC 3 ms
9,484 KB
testcase_02 AC 3 ms
9,456 KB
testcase_03 AC 3 ms
9,384 KB
testcase_04 AC 3 ms
9,496 KB
testcase_05 AC 3 ms
9,512 KB
testcase_06 AC 6 ms
9,564 KB
testcase_07 AC 101 ms
11,796 KB
testcase_08 AC 102 ms
11,832 KB
testcase_09 AC 101 ms
11,732 KB
testcase_10 AC 101 ms
11,788 KB
testcase_11 AC 82 ms
11,844 KB
testcase_12 AC 82 ms
11,820 KB
testcase_13 AC 102 ms
11,788 KB
testcase_14 AC 103 ms
11,812 KB
testcase_15 AC 55 ms
11,812 KB
testcase_16 AC 80 ms
11,820 KB
testcase_17 AC 108 ms
11,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> Pl;
const ll INF=1e18;
const int MAX_N=1<<17;
ll mn1[2*MAX_N-1], mn2[2*MAX_N], mn[2*MAX_N];
int m;
int n;
ll c[100010], d[100010];
void init(){
    c[0]=INF;
	m=1;
	while(m<n) m*=2;
	for(int i=0; i<2*m-1; i++){
		mn[i]=mn1[i]=mn2[i]=INF;
	}
	for(int i=0; i<n; i++){
		mn2[i+m-1]=c[i];
	}
	for(int i=m-2; i>=0; i--){
		mn2[i]=min(mn2[2*i+1], mn2[2*i+2]);
	}
}
void update(int i, ll a){
    i+=m-1;
    mn1[i]=a;
    while(i>0){
        i=(i-1)/2;
        mn1[i]=min(mn1[2*i+1], mn1[2*i+2]);
        mn[i]=min({mn[2*i+1], mn[2*i+2], mn1[2*i+1]+mn2[2*i+2]});
    }
}
Pl query(int a, int b, int k, int l, int r){
	if(r<=a || b<=l) return Pl(INF, P(INF, INF));
	if(a<=l && r<=b){
		return Pl(mn[k], P(mn1[k], mn2[k]));
	}else{
		Pl pl=query(a, b, k*2+1, l, (l+r)/2);
		Pl pr=query(a, b, k*2+2, (l+r)/2, r);
		return Pl(min({pl.first, pr.first, pl.second.first+pr.second.second}), P(min(pl.second.first, pr.second.first), min(pl.second.second, pr.second.second)));
	}
}
ll s[100010];
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++){
        cin>>c[i]>>d[i];
        s[i]=s[i-1]+c[i]+d[i];
    }
    n++;
    init();
    update(0, -d[1]);
    ll dp;
    for(int i=1; i<n; i++){
        dp=s[i]+query(0, i+1, 0, 0, m).first;
        if(i<n-1) update(i, dp-s[i]-d[i+1]);
    }
    cout<<dp<<endl;
    return 0;
}
0