/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 532.0 KiB
#2 Accepted 2ms 360.0 KiB
#3 Accepted 180ms 692.0 KiB
#4 Accepted 119ms 584.0 KiB
#5 Accepted 198ms 596.0 KiB
#6 Accepted 50ms 576.0 KiB
#7 Accepted 53ms 532.0 KiB
#8 Accepted 79ms 532.0 KiB
#9 Accepted 96ms 576.0 KiB
#10 Accepted 28ms 568.0 KiB
#11 Accepted 13ms 532.0 KiB
#12 Accepted 770ms 624.0 KiB
#13 Time Exceeded ≥1000ms ≥576.0 KiB
#14 Time Exceeded ≥1000ms ≥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];
  }
  map<int, vector<int>> who;
  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);
      }
    }
  }
  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:51:49
Judged At
2025-02-17 09:51:49
Judged By
Score
70
Total Time
≥1000ms
Peak Memory
≥692.0 KiB