結果

問題 No.2546 Many Arithmetic Sequences
ユーザー milanis48663220milanis48663220
提出日時 2023-11-25 01:13:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 3,927 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 136,508 KB
実行使用メモリ 25,560 KB
最終ジャッジ日時 2023-11-25 20:45:30
合計ジャッジ時間 6,349 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 57 ms
7,724 KB
testcase_04 AC 117 ms
18,200 KB
testcase_05 AC 92 ms
14,032 KB
testcase_06 AC 20 ms
6,548 KB
testcase_07 AC 44 ms
8,560 KB
testcase_08 AC 12 ms
6,548 KB
testcase_09 AC 49 ms
12,424 KB
testcase_10 AC 89 ms
19,172 KB
testcase_11 AC 99 ms
22,600 KB
testcase_12 AC 39 ms
9,232 KB
testcase_13 AC 74 ms
12,768 KB
testcase_14 AC 84 ms
18,696 KB
testcase_15 AC 25 ms
7,428 KB
testcase_16 AC 77 ms
17,168 KB
testcase_17 AC 110 ms
14,176 KB
testcase_18 AC 58 ms
11,312 KB
testcase_19 AC 67 ms
11,252 KB
testcase_20 AC 43 ms
9,956 KB
testcase_21 AC 49 ms
7,544 KB
testcase_22 AC 90 ms
12,724 KB
testcase_23 AC 161 ms
25,560 KB
testcase_24 AC 158 ms
25,560 KB
testcase_25 AC 160 ms
25,560 KB
testcase_26 AC 161 ms
25,560 KB
testcase_27 AC 159 ms
25,556 KB
testcase_28 AC 144 ms
22,268 KB
testcase_29 AC 146 ms
22,500 KB
testcase_30 AC 145 ms
22,248 KB
testcase_31 AC 146 ms
22,268 KB
testcase_32 AC 145 ms
22,248 KB
testcase_33 AC 2 ms
6,548 KB
testcase_34 AC 131 ms
22,252 KB
testcase_35 AC 2 ms
6,548 KB
testcase_36 AC 99 ms
21,360 KB
testcase_37 AC 2 ms
6,548 KB
testcase_38 AC 2 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

/**
 * verified: https://atcoder.jp/contests/abc228/submissions/27464591
 */ 
template<typename T>
class ConvexHullTrick{
    public:
    T x;
    ConvexHullTrick<T>(){
        x = 0;
    }
    /**
     * xを一つ進めます
     */ 
    void next(){
        x++;
        while(dq.size() >= 2 && f(x, dq[0]) >= f(x, dq[1])) dq.pop_front();
    }
    /**
     * xを指定された値まで進めます
     */ 
    void proceed(T _x){
        assert(x <= _x);
        x = _x;
        while(dq.size() >= 2 && f(x, dq[0]) >= f(x, dq[1])) dq.pop_front();
    }
    /**
     * 直線 y = p_add*x+q を追加します(今まで追加した中で傾きが最小の直線であること)
     */ 
    void add_line(T p_add, T q_add){
        if(!dq.empty()) assert(p_add <= p[dq.back()]);
        p.push_back(p_add);
        q.push_back(q_add);
        int n_lines = p.size();
        while(dq.size() >= 2 && check(dq[dq.size()-2], dq.back(), n_lines-1)) {
            dq.pop_back();
        }
        dq.push_back(n_lines-1);
    }
    T get_min(){
        return f(x, dq[0]);
    }
    private:
    vector<T> p, q; // 直線p[i]x+q[i]
    deque<int> dq;
    T f(int x, int i){
        return p[i]*x+q[i];
    }
    bool check(int i, int j, int k){
        return (p[j]-p[i])*(q[k]-q[j]) >= (q[j]-q[i])*(p[k]-p[j]);
    };
};

using P = pair<ll, ll>;
const ll inf = 4e18;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; ll m; cin >> n >> m;
    vector<ll> a(n), d(n);
    vector<int> pos, neg;
    for(int i = 0; i < n; i++){
        cin >> a[i] >> d[i]; a[i] *= 2; d[i] *= 2;
        if(d[i] >= 0){
            pos.push_back(i);
        }else{
            neg.push_back(i);
        }
    }
    ll ans = -inf;
    if(neg.empty()){
        for(int i = 0; i < n; i++){
            ll sum = (d[i]/2)*m*m-(d[i]/2)*m+a[i]*m;
            chmax(ans, sum);
        }
        cout << ans/2 << endl;
        return 0;
    }
    vector<ll> sum_neg(m+1);
    priority_queue<P> que;
    vector<ll> cnt(n);
    for(int i: neg){
        que.push({a[i], i});
    }
    for(int i = 0; i < m; i++){
        auto [x, idx] = que.top(); que.pop();
        cnt[idx]++;
        sum_neg[i+1] = sum_neg[i]+x;
        que.push({a[idx]+cnt[idx]*d[idx], idx});
    }
    if(pos.empty()){
        cout << sum_neg[m]/2 << endl;
        return 0;
    }
    vector<P> vp;
    for(int i: pos){
        vp.push_back({-d[i]/2, d[i]/2-a[i]});
    }
    sort(vp.begin(), vp.end(), greater<P>());
    ConvexHullTrick<ll> cht;
    for(auto [p, q]: vp){
        cht.add_line(p, q);
    }
    for(ll x = 0; x <= m; x++){
        cht.proceed(x);
        chmax(ans, sum_neg[m-x]-cht.get_min()*x);
    }
    cout << ans/2 << endl;
}
0