結果

問題 No.2546 Many Arithmetic Sequences
ユーザー milanis48663220milanis48663220
提出日時 2023-11-25 01:13:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,926 bytes
コンパイル時間 1,455 ms
コンパイル使用メモリ 136,508 KB
実行使用メモリ 24,952 KB
最終ジャッジ日時 2023-11-25 01:32:00
合計ジャッジ時間 12,479 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 2 ms
6,676 KB
testcase_34 RE -
testcase_35 AC 2 ms
6,676 KB
testcase_36 RE -
testcase_37 AC 2 ms
6,676 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