Submission #489904

# Submission time Handle Problem Language Result Execution time Memory
489904 2021-11-25T03:12:43 Z fhvirus Sirni (COCI17_sirni) C++17
140 / 140
1704 ms 534524 KB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;} 
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif
 
struct DSU {
	int n; vector<int> f;
	DSU (int nn) : n(nn), f(nn) { iota(AI(f), 0); }
	int F(int u) { return u == f[u] ? u : f[u] = F(f[u]); }
};

const int kC = 1e7;
vector<pii> bkt[kC];
 
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 
	int N; cin >> N;
	vector<int> P(N); for(int &i: P) cin >> i;
 
	sort(AI(P));
	P.erase(unique(AI(P)), end(P));
	N = P.size();
 
	if(N <= 1000){
		for(int i = 0; i < N; ++i)
			for(int j = i+1; j < N; ++j)
				bkt[min(P[j] % P[i], P[i])].pb(i, j);
	} else {
		for(int i = 0; i < N-1; ++i){
			for(int j = P[i], lst = i+1; j <= P.back(); j += P[i]){
				int p = lower_bound(begin(P)+lst, end(P), j) - begin(P);
				int r = P[p] / P[i];
				bkt[P[p] - P[i] * r].pb(i, p);
				j = P[i] * r;
				lst = p+1;
			}
		}
	}
 
	DSU dsu(N);
	int ans = 0;
	int cnt = N;
	for(int w = 0; w < P.back(); ++w){
		for(auto &[u, v]: bkt[w]){
			u = dsu.F(u);
			v = dsu.F(v);
			if(u != v){
				dsu.f[v] = u;
				ans += w;
				--cnt;
			}
			if(cnt == 1) goto jizz;
		}
	}

jizz:
	cout << ans << '\n';
 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 209 ms 247572 KB Output is correct
2 Correct 155 ms 241756 KB Output is correct
3 Correct 251 ms 248656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 240328 KB Output is correct
2 Correct 136 ms 237816 KB Output is correct
3 Correct 230 ms 249124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 249360 KB Output is correct
2 Correct 200 ms 245276 KB Output is correct
3 Correct 221 ms 249180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 245004 KB Output is correct
2 Correct 370 ms 272240 KB Output is correct
3 Correct 221 ms 250104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 236788 KB Output is correct
2 Correct 243 ms 257348 KB Output is correct
3 Correct 222 ms 245020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 304 ms 257248 KB Output is correct
2 Correct 460 ms 287052 KB Output is correct
3 Correct 217 ms 248784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 238588 KB Output is correct
2 Correct 400 ms 281524 KB Output is correct
3 Correct 214 ms 248120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 248132 KB Output is correct
2 Correct 1141 ms 443732 KB Output is correct
3 Correct 238 ms 250272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 247608 KB Output is correct
2 Correct 1704 ms 534524 KB Output is correct
3 Correct 247 ms 254172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 237368 KB Output is correct
2 Correct 1672 ms 530540 KB Output is correct
3 Correct 223 ms 249892 KB Output is correct