結果

問題 No.860 買い物
コンテスト
ユーザー chocorusk
提出日時 2019-08-09 23:07:44
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 90 ms / 1,000 ms
コード長 1,773 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 679 ms
コンパイル使用メモリ 130,820 KB
実行使用メモリ 142,768 KB
最終ジャッジ日時 2026-04-02 17:14:34
合計ジャッジ時間 2,415 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ostream:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/iostream:43,
                 from main.cpp:3:
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>]',
    inlined from 'int main()' at main.cpp:79:11:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ostream.h:212:25: warning: 'dp' may be used uninitialized [-Wmaybe-uninitialized]
  212 |       { return _M_insert(__n); }
      |                ~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:74:8: note: 'dp' was declared here
   74 |     ll dp;
      |        ^~

ソースコード

diff #
raw source code

#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