/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 150ms 592.0 KiB
#4 Accepted 116ms 532.0 KiB
#5 Accepted 189ms 532.0 KiB
#6 Accepted 47ms 588.0 KiB
#7 Accepted 53ms 592.0 KiB
#8 Accepted 79ms 580.0 KiB
#9 Accepted 92ms 588.0 KiB
#10 Accepted 27ms 532.0 KiB
#11 Accepted 12ms 588.0 KiB
#12 Accepted 754ms 624.0 KiB
#13 Time Exceeded ≥1098ms ≥532.0 KiB
#14 Time Exceeded ≥1100ms ≥532.0 KiB

Code

#include <bits/stdc++.h>

using namespace std;

struct dsu {
  vector<int> par, rank, sz;
  int cnt;
  dsu(int n) {
    par.resize(n);
    iota(par.begin(), par.end(), 0);
    rank.resize(n, 0);
    sz.resize(n, 1);
    cnt = n;
  }
  int find(int x) {
    return x == par[x] ? x : par[x] = find(par[x]);
  }
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y);
    par[y] = x, sz[x] += sz[y];
    if (rank[x] == rank[y]) rank[x] += 1;
    --cnt; return true;
  }
  int size(int x) {
    return sz[find(x)];
  }
  int sets() {
    return cnt;
  }
  bool isSame(int x, int y) {
    return find(x) == find(y);
  }
};

void solve(int cs) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  dsu d(n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (__gcd(a[i], a[j]) > 1) {
        d.unite(i, j);
      }
    }
  }
  vector<vector<int>> who(n);
  for (int i = 0; i < n; i++) {
    who[d.find(i)].push_back(a[i]);
  }
  for (auto &v : who) {
    sort(v.rbegin(), v.rend());
  }
  int64_t cur = 0, ans = 0;
  vector<int> fu(n, 0);
  for (int i = 0; i < k; i++) {
    int u = d.find(i);
    cur += who[u][fu[u]];
    fu[u] += 1;
  }
  ans = cur;
  for (int i = k; i < n; i++) {
    int u = d.find(i - k);
    fu[u] -= 1;
    cur -= who[u][fu[u]];
    u = d.find(i);
    cur += who[u][fu[u]];
    fu[u] += 1;
    ans = max(ans, cur);
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(!cin.tie(0));
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve(i);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1063 Another Maximum Sum in Subarray
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 09:52:59
Judged At
2025-02-17 09:52:59
Judged By
Score
70
Total Time
≥1100ms
Peak Memory
≥624.0 KiB