結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー nola_suznola_suz
提出日時 2016-11-18 23:14:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 5,191 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 113,712 KB
実行使用メモリ 16,400 KB
最終ジャッジ日時 2023-09-02 12:50:31
合計ジャッジ時間 6,691 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 31 ms
4,968 KB
testcase_14 AC 35 ms
5,160 KB
testcase_15 AC 89 ms
8,488 KB
testcase_16 AC 152 ms
11,960 KB
testcase_17 AC 164 ms
12,812 KB
testcase_18 AC 25 ms
4,572 KB
testcase_19 AC 211 ms
15,216 KB
testcase_20 AC 72 ms
7,272 KB
testcase_21 AC 208 ms
15,064 KB
testcase_22 AC 74 ms
7,600 KB
testcase_23 AC 210 ms
15,260 KB
testcase_24 AC 50 ms
6,100 KB
testcase_25 AC 205 ms
15,272 KB
testcase_26 AC 24 ms
4,548 KB
testcase_27 AC 153 ms
12,276 KB
testcase_28 AC 142 ms
11,292 KB
testcase_29 AC 80 ms
8,024 KB
testcase_30 AC 216 ms
15,424 KB
testcase_31 AC 30 ms
4,940 KB
testcase_32 AC 34 ms
5,104 KB
testcase_33 AC 237 ms
16,240 KB
testcase_34 AC 226 ms
16,256 KB
testcase_35 AC 236 ms
16,304 KB
testcase_36 AC 228 ms
16,120 KB
testcase_37 AC 237 ms
16,400 KB
testcase_38 AC 235 ms
16,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

template<typename T>
class range_minimum_query{
public:
    std::vector<T> data;
    std::vector<std::vector<int> > block;
    std::vector<int> sblock;
    std::vector<int> lookup;
    int N;
    int lg;
    int min(int a,int b){
        return data[b]<data[a]?b:a;
    }
    range_minimum_query(){}
    range_minimum_query(std::vector<T> &t){
        data=t;
        N=data.size();
        lg=32-__builtin_clz(N);
        block.assign((N+lg-1)/lg,std::vector<int>(lg,0));
        for(int i=0;i<N;i++){
            if(i%lg==0)
                block[i/lg][0]=i;
            block[i/lg][0]=min(i,block[i/lg][0]);
        }
        for(int j=1;j<lg;j++)
            for(int i=0;i<block.size();i++)
                block[i][j]=min(block[i][j-1],block[i+(i+(1<<(j-1))<block.size()?(1<<(j-1)):0)][j-1]);
        sblock.assign(N,0);
        std::vector<int> st;
        for(int i=0;i<block.size();i++){
            st.clear();
            for(int j=i*lg;j<N&&j<i*lg+lg;j++){
                while(!st.empty()&&data[j]<data[st.back()])
                    st.pop_back();
                sblock[j]=1<<(i*lg+lg-j-1);
                if(!st.empty())
                    sblock[j]|=sblock[st.back()];
                st.push_back(j);
            }
        }
        lookup.assign(1<<lg,0);
        for(int i=1,ans=lg-1;i<(1<<lg);i++){
            if(1<<(lg-ans)<=i)
                ans--;
            lookup[i]=ans;
        }
    }
    T q(int l,int r){
        if(r<l)
            swap(l,r);
        int l1=l/lg+1;
        int r1=r/lg-1;
        int ans=l;
        int t;
        if(l1<=r1){
            t=lg-lookup[r1-l1+1]-1;
            ans=min(ans,min(block[l1][t],block[r1-(1<<t)+1][t]));
        }
        t=l1*lg-1<r?l1*lg-1:r;
        ans=min(ans,lookup[sblock[t]&(~(((1<<(l-(l1*lg-lg)))-1)<<(l1*lg-l)))]+l1*lg-lg);
        t=r;
        l=l>r1*lg+lg?l:r1*lg+lg;
        ans=min(ans,lookup[sblock[t]&(~(((1<<(l-(r1*lg+lg)))-1)<<(r1*lg+(lg<<1)-l)))]+r1*lg+lg);
        return data[ans];
    }
};



vector<pll> v;
ll K;
int n;
vll sum;
vint nex;

ll loss(int a, int b){
	if(a>b) return 0;
	ll ret = sum[b];
	if(a-1>=0) ret -= sum[a-1];
	return ret;
}

ll calc(ll m, range_minimum_query<ll> &rmq){
	vll dp(n+1,INFL);
	dp[0] = 0;
	rep(i,n){
		if(v[i].S>m){
			if(i+1>nex[i]-1 || -rmq.q(i+1, nex[i]-1) <= m) mins(dp[nex[i]], dp[i]+loss(i+1,nex[i]-1));
		}
		else{
			mins(dp[i+1], dp[i]+v[i].S);
			if(i+1>nex[i]-1 || -rmq.q(i+1, nex[i]-1) <= m) mins(dp[nex[i]], dp[i]+loss(i+1,nex[i]-1));
		}
	}
	if(dp[n] == INFL){
		return -1;
	}
	return dp[n];
}


void mainmain(){
	cin>>n;
	cin>>K;
	v=vector<pll>(n);
	sum = vll(n);
	nex = vint(n);
	vll tmp(n);
	rep(i,n) cin>>v[i].F>>v[i].S,tmp[i]=-v[i].S;
	range_minimum_query<ll> rmq(tmp);
	rep(i,n){
		if(!i) sum[i] = v[i].S;
		else{
			sum[i] = sum[i-1]+v[i].S;
		}
	}
	int p = 0;
	rep(i,n){
		while(p<n&&v[p].F<v[i].F+K){
			p++;
		}
		nex[i]=p;
	}
	ll l = -1;
	ll r = INT_MAX;
	ll ans = INFL;
	while(r-l>1){
		ll mid = (l+r)/2;
		ll t = calc(mid, rmq);
		if(t<0){
			l = mid;
		}
		else{
			r = mid;
			ans = t;
		}
	}
	cout<<r<<endl;
	cout<<ans<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}
0