結果
問題 | No.2321 Continuous Flip |
ユーザー | milanis48663220 |
提出日時 | 2023-05-26 22:45:05 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 462 ms / 2,000 ms |
コード長 | 4,125 bytes |
コンパイル時間 | 2,082 ms |
コンパイル使用メモリ | 145,160 KB |
実行使用メモリ | 64,620 KB |
最終ジャッジ日時 | 2024-12-25 09:10:43 |
合計ジャッジ時間 | 16,182 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 30 |
ソースコード
#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; }