Advertisement
IanisBelu

G. Four Melodies

Oct 28th, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | Source Code | 0 0
  1. #pragma GCC optimize("Ofast,unroll-loops")
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <queue>
  6. #include <cstring>
  7.  
  8. #define sz(a) ((int)(a).size())
  9. #define all(a) (a).begin(), (a).end()
  10.  
  11. #define fi first
  12. #define se second
  13.  
  14. #define lsb(x) (x & (-x))
  15.  
  16. using namespace std;
  17.  
  18. inline bool ckmin(int &a, int b) { if (a <= b) return false; else a = b; return true; }
  19. inline bool ckmax(int &a, int b) { if (a >= b) return false; else a = b; return true; }
  20.  
  21. typedef long long ll;
  22. typedef pair<int, int> pii;
  23.  
  24. const int NMAX = 6005;
  25. const int INF = 1e9+5;
  26.  
  27. struct Edge {
  28.     int dest, cap, cost, other;
  29. };
  30.  
  31. int n;
  32. int a[NMAX];
  33. int source, sink;
  34. vector<Edge> g[NMAX];
  35. int dist[NMAX], pi[NMAX], pi2[NMAX];
  36. int vis[NMAX];
  37. bool inq[NMAX];
  38. pii nxt[NMAX];
  39.  
  40. void add_edge(int x, int y, int cap, int cost) {
  41.     if (!cap) return;
  42.     g[x].push_back({ y, cap, cost, sz(g[y]) });
  43.     g[y].push_back({ x, 0, -cost, sz(g[x]) - 1 });
  44. }
  45.  
  46. void read() {
  47.     cin >> n;
  48.     for (int i = 1; i <= n; i++)
  49.         cin >> a[i];
  50. }
  51.  
  52. void init_pi() {
  53.     for (int i = 1; i <= n; i++)
  54.         pi[i] = INF;
  55.     pi[source] = 0;
  56.  
  57.     for (int i = 1; i <= ((n - 2) >> 1); i++) {
  58.         for (auto &it : g[i]) {
  59.             if (!it.cap) ckmin(pi[i], pi[it.dest] - it.cost);
  60.         }
  61.         int u = i + ((n - 2) >> 1);
  62.         for (auto &it : g[u]) {
  63.             if (it.dest < u) ckmin(pi[u], pi[it.dest] - it.cost);
  64.         }
  65.     }
  66. }
  67.  
  68. void dijkstra() {
  69.     for (int i = 1; i <= n; i++) {
  70.         dist[i] = INF;
  71.         vis[i] = false;
  72.         pi2[i] = pi[i];
  73.     }
  74.  
  75.     priority_queue<pii> pq;
  76.  
  77.     pq.push({0, source});
  78.     dist[source] = pi[source] = 0;
  79.     nxt[source] = {0, 0};
  80.     int i;
  81.  
  82.     while (!pq.empty()) {
  83.         int u = pq.top().se;
  84.  
  85.         pq.pop();
  86.  
  87.         if (vis[u]) continue;
  88.         vis[u] = true;
  89.  
  90.         i = 0;
  91.         for (auto &it : g[u]) {
  92.             if (it.cap && ckmin(dist[it.dest], dist[u] + it.cost + pi2[u] - pi2[it.dest])) {
  93.                 pi[it.dest] = pi[u] + it.cost;
  94.                 nxt[it.dest] = { u, i };
  95.                 pq.push({-dist[it.dest], it.dest});
  96.             }
  97.             i++;
  98.         }
  99.     }
  100. }
  101.  
  102. int mcf(int flow) {
  103.     int min_cost = 0;
  104.     init_pi();
  105.     while (dijkstra(), dist[sink] != INF && flow) {
  106.         int f = flow, cost = 0;
  107.         for (int u = sink; u != source; u = nxt[u].fi) {
  108.             Edge &e = g[nxt[u].fi][nxt[u].se];
  109.             cost += e.cost;
  110.             ckmin(f, e.cap);
  111.         }
  112.         for (int u = sink; u != source; u = nxt[u].fi) {
  113.             Edge &e = g[nxt[u].fi][nxt[u].se];
  114.             e.cap -= f;
  115.             g[u][e.other].cap += f;
  116.         }
  117.         flow -= f;
  118.         min_cost += f * cost;
  119.     }
  120.     return min_cost;
  121. }
  122.  
  123. int solve() {
  124.     source = 2 * n + 1, sink = 2 * n + 2;
  125.     for (int i = 1; i <= n; i++) {
  126.         add_edge(source, i, 1, n);
  127.         add_edge(i, n + i, 1, -1);
  128.         add_edge(n + i, sink, 1, n);
  129.         for (int j = 1; j < i; j++) {
  130.             if (abs(a[i] - a[j]) <= 1 || a[i] % 7 == a[j] % 7)
  131.                 add_edge(n + j, i, 1, 0);
  132.         }
  133.     }
  134.     n += n + 2;
  135.     return -mcf(4) + 8 * ((n - 2) / 2);
  136. }
  137.  
  138. signed main() {
  139. #ifdef LOCAL
  140.     freopen("input.txt", "r", stdin);
  141. #else
  142.     ios_base::sync_with_stdio(false);
  143.     cin.tie(0);
  144.     cout.tie(0);
  145. #endif
  146.     read();
  147.     cout << solve() << endl;
  148.     return 0;
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement