답안 #89213

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
89213 2018-12-11T01:21:48 Z jasony123123 Sirni (COCI17_sirni) C++11
112 / 140
5000 ms 670320 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
inline void io() {
#ifdef LOCAL_PROJECT 
	freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else 
	/* online submission */
#endif 
	ios_base::sync_with_stdio(false); cin.tie(NULL);
}
/**************************COCI 2016-2017 R6 P4*************************/

template <int SZ> struct UF {
	int N; // Vertices 0...N-1
	int par[SZ], compsz[SZ]; // parent of i, size of component
	UF() {
		FOR(i, 0, SZ) par[i] = i, compsz[i] = 1;
	}
	int find(int x) { // path compression
		if (par[x] != x) par[x] = find(par[x]);
		return par[x];
	}
	bool unite(int x, int y) { // unite by rank. return TRUE if united. FALSE if already same component
		x = find(x), y = find(y);
		if (x == y) return false;
		if (compsz[x] < compsz[y]) swap(x, y); // now x is the larger component
		compsz[x] += compsz[y];
		par[y] = x;
		return true;
	}
};

const int MAXP = 10000001;
const int MAXN = 100001;
const int thresh = 100000;

int N;
int M;
int nxt[MAXP];
v<int> P;
v<pii> edgeSorted[MAXP];
UF<MAXN> uf;

void input() {
	// input
	sf("%d", &N);
	P.resize(N);
	FOR(i, 0, N) sf("%d", &P[i]);
	sort(P.begin(), P.end());
	P.erase(unique(P.begin(), P.end()), P.end());
	N = P.size();
	M = P.back();
}

void comNxt() {
	// compute nxt
	for (int i = M, j = P.size() - 1; i >= P.front(); i--) {
		if (j >= 0 && i == P[j])
			nxt[i] = j--;
		else
			nxt[i] = nxt[i + 1];
	}
}

void makeEd() {
	// compute edges
	pii e;
	int mod;
	FOR(i, 0, N) {
		e = { i, nxt[P[i] + 1] };
		mod = P[e.second] % P[e.first];
		if (mod<thresh)
			edgeSorted[mod].emplace_back(e);
		for (int k = P[i] + P[i]; k <= M; k += P[i]) {
			e = { i, nxt[k] };
			mod = P[e.second] % P[e.first];
			if(mod<thresh)
				edgeSorted[mod].emplace_back(e);
		}
	}
}

ll tree() {
	// mst
	int num = 0;
	ll mstcost = 0;
	FORE(c, 0, M) {
		for (pii e : edgeSorted[c]) {
			if (uf.unite(e.first, e.second)) {
				mstcost += (ll)(c);
				num++;
			}
			if (num == N - 1) {
				return mstcost;
			}
		}
	}
	assert(0);
	return mstcost;
}

int main() {
	io();
	input();
	comNxt();
	makeEd();
	pf("%lld\n", tree());
}

Compilation message

sirni.cpp: In function 'void input()':
sirni.cpp:71:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d", &N);
    ^
sirni.cpp:73:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  FOR(i, 0, N) sf("%d", &P[i]);
                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 292 ms 275292 KB Output is correct
2 Correct 745 ms 304524 KB Output is correct
3 Correct 264 ms 304524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 304524 KB Output is correct
2 Correct 2846 ms 670320 KB Output is correct
3 Correct 292 ms 670320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 265 ms 670320 KB Output is correct
2 Correct 268 ms 670320 KB Output is correct
3 Correct 269 ms 670320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 281 ms 670320 KB Output is correct
2 Correct 380 ms 670320 KB Output is correct
3 Correct 298 ms 670320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 237 ms 670320 KB Output is correct
2 Correct 402 ms 670320 KB Output is correct
3 Correct 277 ms 670320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 327 ms 670320 KB Output is correct
2 Correct 441 ms 670320 KB Output is correct
3 Correct 307 ms 670320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 670320 KB Output is correct
2 Correct 435 ms 670320 KB Output is correct
3 Correct 330 ms 670320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 504 ms 670320 KB Output is correct
2 Correct 3988 ms 670320 KB Output is correct
3 Correct 502 ms 670320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 495 ms 670320 KB Output is correct
2 Execution timed out 5150 ms 670320 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 670320 KB Output is correct
2 Execution timed out 5134 ms 670320 KB Time limit exceeded
3 Halted 0 ms 0 KB -