結果

問題 No.2321 Continuous Flip
ユーザー milanis48663220milanis48663220
提出日時 2023-05-26 22:45:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 4,125 bytes
コンパイル時間 1,562 ms
コンパイル使用メモリ 143,060 KB
実行使用メモリ 65,664 KB
最終ジャッジ日時 2023-08-26 12:56:46
合計ジャッジ時間 11,751 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 318 ms
57,684 KB
testcase_05 AC 305 ms
57,560 KB
testcase_06 AC 295 ms
57,684 KB
testcase_07 AC 284 ms
57,620 KB
testcase_08 AC 287 ms
57,640 KB
testcase_09 AC 289 ms
57,700 KB
testcase_10 AC 305 ms
57,696 KB
testcase_11 AC 293 ms
57,612 KB
testcase_12 AC 299 ms
57,628 KB
testcase_13 AC 316 ms
57,616 KB
testcase_14 AC 319 ms
57,608 KB
testcase_15 AC 311 ms
57,544 KB
testcase_16 AC 310 ms
57,616 KB
testcase_17 AC 316 ms
57,756 KB
testcase_18 AC 303 ms
57,672 KB
testcase_19 AC 299 ms
57,708 KB
testcase_20 AC 315 ms
57,560 KB
testcase_21 AC 303 ms
57,632 KB
testcase_22 AC 311 ms
57,688 KB
testcase_23 AC 320 ms
57,668 KB
testcase_24 AC 126 ms
51,424 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 116 ms
51,536 KB
testcase_27 AC 119 ms
54,256 KB
testcase_28 AC 229 ms
65,096 KB
testcase_29 AC 226 ms
65,392 KB
testcase_30 AC 229 ms
64,888 KB
testcase_31 AC 217 ms
65,664 KB
testcase_32 AC 217 ms
64,924 KB
testcase_33 AC 226 ms
64,728 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>
#include <random>

std::random_device rnd;
std::mt19937 mt(rnd());

int randint(const int l, const int r){
    return mt()%(r - l) + l;
}

#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;
}

const ll INF = 1e+18;

using P = pair<ll, int>;

struct edge{
    int to;
    ll cost;
    edge(int to, ll cost): to(to), cost(cost) {}
};

vector<ll> dijkstra(int s, vector<vector<edge>> G){
    priority_queue<P, vector<P>, greater<P>> que;
    int n = G.size();
    vector<ll> d(n, INF);
    d[s] = 0;
    que.push(P(0, s));
    while(!que.empty()){
        P p = que.top();que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        for(int i = 0; i < G[v].size(); i++){
            edge e = G[v][i];
            if(d[v] + e.cost < d[e.to]){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
    return d;
}

ll solve(vector<ll> a, vector<int> l, vector<int> r, ll c){
    int n = a.size(), m = l.size();
    vector<vector<edge>> g(n+1);
    for(int i = 0; i < n; i++){
        g[i].push_back(edge(i+1, a[i]));
        g[i+1].push_back(edge(i, a[i]));
    }
    for(int i = 0; i < m; i++){
        g[l[i]].push_back(edge(r[i], c));
        g[r[i]].push_back(edge(l[i], c));
    }
    ll a_sum = accumulate(a.begin(), a.end(), 0ll);
    auto d = dijkstra(0, g);
    return a_sum-d[n];
}

ll naive(vector<ll> a, vector<int> l, vector<int> r, ll c){
    int n = a.size(), m = l.size();
    ll ans = 0;
    for(int s = 0; s < (1<<m); s++){
        vector<int> f(n);
        ll tmp = 0;
        for(int i = 0; i < m; i++){
            if(s&(1<<i)){
                for(int j = l[i]; j < r[i]; j++){
                    f[j] ^= 1;
                }
                tmp -= c;
            }
        }
        for(int i = 0; i < n; i++){
            tmp += a[i]*f[i];
        }
        chmax(ans, tmp);
    }
    return ans;
}

void test(int n, int m, ll c){
    vector<ll> a(n);
    vector<int> l(m), r(m);
    for(int i = 0; i < n; i++){
        a[i] = randint(1, 100);
    }
    for(int i = 0; i < m; i++){
        l[i] = randint(0, n);
        r[i] = randint(0, n);
        while(r[i] == l[i]){
            r[i] = randint(0, n);
        }
        if(l[i] > r[i]) swap(l[i], r[i]);
    }
    ll a0 = solve(a, l, r, c);
    ll a1 = naive(a, l, r, c);
    if(a0 != a1){
        cout << "=====" << endl;
        print_vector(a);
        print_vector(l);
        print_vector(r);
        cout << a0 << ' ' << a1 << endl;
    }
}

void run_tests(int iter){
    while(iter--){
        test(20, 15, 5);
    }
    cout << "OK" << endl;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    // int x; cin >> x;
    // run_tests(x);
    int n, m; ll c; cin >> n >> m >> c;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<int> l(m), r(m);
    for(int i = 0; i < m; i++){
        cin >> l[i] >> r[i]; l[i]--;
    }
    cout << solve(a, l, r, c) << endl;
}
0