답안 #1004139

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004139 2024-06-21T05:42:05 Z fuad27 Sirni (COCI17_sirni) C++17
112 / 140
1324 ms 786432 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+10;
int suff[N];
int pos[N];
struct DSU {
  vector<int> e;
  DSU(int n) {
    e=vector<int>(n,-1);
  }
  int get(int a) {
    if(e[a] < 0)return a;
    return e[a]=get(e[a]);
  }
  int unite(int a, int b) {
    a=get(a),b=get(b);
    if(a==b)return 0;
    if(-e[a] > -e[b])swap(a,b);
    e[b]+=e[a];
    e[a] = b;
    return 1;
  }
};
int main () {
  memset(suff, -1, sizeof suff);
  int n;
  cin >> n;
  set<int> s;
  for(int i = 0;i<n;i++) {
    int v;
    cin >> v;
    s.insert(v);
  }
  vector<int> v(s.begin(), s.end());
  {
    int cnt=0;
    for(int i:v) {
      suff[i] =i;
      pos[i] = cnt++;
    }
  }
  for(int i = N-2;i>=0;i--) {
    if(suff[i] == -1)suff[i] = suff[i+1];
  }
  vector<pair<int,int>> e[N];
  for(int i:v) {
    for(int j = i;j<N;j+=i) {
      if(suff[j] == j) {
        e[0].push_back({j, i});
        if(suff[j+1]!=-1) {
          e[suff[j+1]-j].push_back({suff[j+1], i});
        }
      }
      else if(suff[j]!=-1){
          e[suff[j]-j].push_back({suff[j], i});
      }
    }
  }
  long long sum=0;
  DSU d(n);
  for(int i = 0;i<N;i++) {
    for(auto [u, x]:e[i]) {
      sum+=i*d.unite(pos[u], pos[x]);
    }
  }
  cout << sum << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 278020 KB Output is correct
2 Correct 315 ms 308932 KB Output is correct
3 Correct 177 ms 278612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 274428 KB Output is correct
2 Runtime error 1324 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 278404 KB Output is correct
2 Correct 197 ms 278476 KB Output is correct
3 Correct 196 ms 278608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 319 ms 295132 KB Output is correct
2 Correct 520 ms 326156 KB Output is correct
3 Correct 373 ms 305352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 281440 KB Output is correct
2 Correct 373 ms 302496 KB Output is correct
3 Correct 348 ms 293472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 407 ms 310984 KB Output is correct
2 Correct 562 ms 344736 KB Output is correct
3 Correct 329 ms 305412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 267 ms 283268 KB Output is correct
2 Correct 553 ms 348096 KB Output is correct
3 Correct 307 ms 307732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 333848 KB Output is correct
2 Correct 1097 ms 695564 KB Output is correct
3 Correct 263 ms 336848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 259 ms 338192 KB Output is correct
2 Runtime error 804 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 198 ms 312916 KB Output is correct
2 Correct 1024 ms 695700 KB Output is correct
3 Correct 317 ms 308180 KB Output is correct