Submission #679716

# Submission time Handle Problem Language Result Execution time Memory
679716 2023-01-09T02:05:39 Z bane Sirni (COCI17_sirni) C++17
140 / 140
3870 ms 748332 KB
#include<bits/stdc++.h>
using namespace std;
           
using ll = long long;
using db = long double;
using llu = unsigned long long;
using str = string;
           
// pairs
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdb = pair<db,db>;
#define mp make_pair
#define fr first
#define sc second
           
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT, size_t SZ> using AR = array<T,SZ>; 
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pll>;
using vpd = V<pdb>;
#define ms0(x) memset(x , 0, sizeof(x))
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sor(x) sort(all(x)) 
#define rsz resize
#define ins insert 
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
           
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
tcT> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); }
           
// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
           
const int MOD = (int)2019201949; // 998244353;
const int MX = (int)2e5+5;
const ll BIG = 1e18; 
const db PI = acos((db)-1);
using ld = long double;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {-1,0,1,0};
#define Mint mi
struct mi {
    int v; explicit operator int() const { return v; }
    mi() { v = 0; }
    mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; }
};
mi& operator+=(mi& a, mi b) { 
    if ((a.v += b.v) >= MOD) a.v -= MOD; 
    return a;
}
mi& operator-=(mi& a, mi b) { 
    if ((a.v -= b.v) < 0) a.v += MOD; 
    return a;
}
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((long long) a.v * b.v); }
mi& operator*=(mi& a, mi b) { return a = a * b; }
mi pow(mi a, long long p) {
    assert(p >= 0);
    return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);
}
mi inv(mi a) { assert(a.v != 0); return pow(a, MOD - 2); }
mi operator/(mi a, mi b) { return a * inv(b); }
struct DSU {
    vector<int> e;
    void init(int N) { e = vector<int>(N, -1); }
   
    int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
   
    bool same_set(int a, int b) { return get(a) == get(b); }
   
    int size(int x) { return -e[get(x)]; }
   
    bool unite(int x, int y) {
        x = get(x), y = get(y);
        if (x == y) return false;
        if (e[x] > e[y]) swap(x, y);
        e[x] += e[y]; e[y] = x; return true;
    }
};
  
void setIO(string name = "") {
    cin.tie(0)->sync_with_stdio(0); 
    if (sz(name)) {
        freopen((name + ".in").c_str(), "r", stdin); 
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
 class DisjointSetUnion {
protected:
    vector<int> parent;
    vector<int> compSize;
    const int n;
    int connectedComponents;
public:
    int getConnectedComponents() const {
        return connectedComponents;
    }
 
public:
    DisjointSetUnion(int sz) : n(sz), connectedComponents(sz) {
        parent.resize(sz), compSize.resize(sz);
        for (int i = 0; i < n; i++) {
            parent[i] = i, compSize[i] = 1;
        }
    }
 
    int find_head(int x) const {
        int cur = x;
        while (cur != parent[cur]) {
            cur = parent[cur];
        }
        return cur;
    }
 
    void join(int x, int y) {
        x = find_head(x);
        y = find_head(y);
        if (x == y) {
            return;
        }
        if (compSize[x] > compSize[y]) {
            swap(x, y);
            //ensures that compSize[x1] <= compSize[y1]
        }
        parent[x] = y;
        compSize[y] += compSize[x];
        connectedComponents--;
    }
 
    bool comp(int x, int y) {
        return (find_head(x) == find_head(y));
    }
};
struct Edge{
	ll cost;
	bool col;
	int id;
};
 
int main() {
	setIO("");
	int n;
	cin >> n;
	vector<int>a(n);
	for (int i = 0; i<n; i++)cin >> a[i];
	sort(a.begin(),a.end());
	a.resize(unique(a.begin(), a.end()) - a.begin());
	DisjointSetUnion dsu(n);
	int MX = 0;
	for (int i =0 ; i<n; i++)MX = max(MX, a[i]);
	++MX;
	int dp[MX + 1];
	for (int i = 0; i<=MX; i++)dp[i]=-1;
	for (int i = 0; i<n; i++){
		dp[a[i]] = i;
	}
	for (int i = MX - 1; i>0; i--){
		if (dp[i] == -1)dp[i] = dp[i + 1];
	}
	//cout<<
	int64_t tot = 0;
	vector<pair<int,int>> edges[MX + 1];
	int N = n;
	for (int i = 0; i < N; i++) {
		if (i != N - 1) {
			edges[a[i + 1] % a[i]].emplace_back(i, i + 1);
		}
		for (int mx = a[i]; mx < MX; mx += a[i]) {
			if (dp[mx] == -1) continue;
			edges[a[dp[mx]] % a[i]].emplace_back(dp[mx], i);
		}
	}
	for (int i = 0; i <= MX; i++) {
		for (auto& e: edges[i]) {
			if (dsu.comp(e.first, e.second)) {
				continue;
			}
			dsu.join(e.first, e.second);
			tot += i;
		}
	}
	cout << tot;
    return 0;   
}

Compilation message

sirni.cpp: In function 'void setIO(std::string)':
sirni.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 199 ms 274124 KB Output is correct
2 Correct 311 ms 302508 KB Output is correct
3 Correct 191 ms 273668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1544 ms 690140 KB Output is correct
3 Correct 191 ms 274984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 214 ms 274320 KB Output is correct
2 Correct 195 ms 273756 KB Output is correct
3 Correct 194 ms 274408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 39040 KB Output is correct
2 Correct 169 ms 67384 KB Output is correct
3 Correct 114 ms 52104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 30036 KB Output is correct
2 Correct 1012 ms 263188 KB Output is correct
3 Correct 65 ms 25568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 51952 KB Output is correct
2 Correct 237 ms 84932 KB Output is correct
3 Correct 106 ms 48340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 9848 KB Output is correct
2 Correct 295 ms 87512 KB Output is correct
3 Correct 122 ms 50004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 309 ms 289084 KB Output is correct
2 Correct 1651 ms 634148 KB Output is correct
3 Correct 376 ms 291696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 346 ms 293532 KB Output is correct
2 Correct 3578 ms 748332 KB Output is correct
3 Correct 516 ms 348476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 218 ms 276940 KB Output is correct
2 Correct 3870 ms 649032 KB Output is correct
3 Correct 121 ms 53372 KB Output is correct