Submission #949206

# Submission time Handle Problem Language Result Execution time Memory
949206 2024-03-19T03:43:07 Z vjudge1 Sirni (COCI17_sirni) C++17
98 / 140
5000 ms 455604 KB
#include <bits/stdc++.h>
 
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define rnd(l, r) uniform_int_distribution<int>(l, r)(rng)
 
using namespace std;

void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);} 
int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;}
int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int N = 1e7 + 2;

int32_t p[N];
bool have[N];
int32_t par[N],sz[N];

int find(int a){
    if(par[a] == a)
        return a;
    return find(par[a]);
}
void connect(int a, int b){
    a = find(a);
    b = find(b);
    if(a==b)
		return;
        if(sz[a] < sz[b])
            swap(a,b);
 
        par[b] = a;
        sz[a] += sz[b];
}

main(){
	iostream::sync_with_stdio(false);	
    cin.tie(nullptr);
    cout.tie(nullptr);
	int n;
	cin >> n;
	set <int32_t> st;
	for(int i = 1; i <= n; i++){
		cin >> p[i];
		have[p[i]] = 1;
		st.insert(p[i]);
	}
	
	vector <array <int32_t, 3> > g;
	for(int i = 1; i < N; i++){
		if(!have[i])continue;
		sz[i] = 1;
		par[i] = i;
		for(int j = i; j < N; j += i){
			auto it = st.lower_bound(j);
			if(it == st.end())break;
			while(*it > j + i){
				j += i;
			}
			int prev = *it;
			g.pb({*it % i, *it, i});
			
			it = st.upper_bound(j);
			if(*it == prev)continue;
			if(it == st.end())break;
			while(*it > j + i){
				j += i;
			}
			g.pb({*it % i, *it, i});
		}
	}
	sort(all(g));
	int ans = 0;
	for(auto &[cost, a, b] : g){
		if(find(a) != find(b)){
			connect(a, b);
			ans += cost;
		}
	}
	cout << ans << endl;
}	
/*
 * Before implementing the problem:
	
	Do I understand the problem correctly?
	Which places are tricky? What do I need to remember to implement them correctly?
	Which place is the heaviest by implementation? Can I do it simpler?
	Which place is the slowest? Where do I need to be careful about constant factors and where I can choose slower but simpler implementation?
	----------------------------------
	If not AC:
 
	Did you remember to do everything to handle the tricky places you thought about before?
	Is the solution correct?
	Do I understand the problem correctly?
	----------------------------------
	If you have a test on which the solution gives wrong answer:
 
	Is the solution doing what it was supposed to do?
	Is the problem in the code or in the idea?
*/

Compilation message

sirni.cpp:41:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   41 | main(){
      | ^~~~
sirni.cpp: In function 'int main()':
sirni.cpp:66:14: warning: narrowing conversion of '(((long long int)((int)it.std::_Rb_tree_const_iterator<int>::operator*())) % i)' from 'long long int' to 'int' [-Wnarrowing]
   66 |    g.pb({*it % i, *it, i});
      |          ~~~~^~~
sirni.cpp:66:24: warning: narrowing conversion of 'i' from 'long long int' to 'int' [-Wnarrowing]
   66 |    g.pb({*it % i, *it, i});
      |                        ^
sirni.cpp:74:14: warning: narrowing conversion of '(((long long int)((int)it.std::_Rb_tree_const_iterator<int>::operator*())) % i)' from 'long long int' to 'int' [-Wnarrowing]
   74 |    g.pb({*it % i, *it, i});
      |          ~~~~^~~
sirni.cpp:74:24: warning: narrowing conversion of 'i' from 'long long int' to 'int' [-Wnarrowing]
   74 |    g.pb({*it % i, *it, i});
      |                        ^
sirni.cpp: In function 'void fp(std::string)':
sirni.cpp:13:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:13:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 51 ms 88660 KB Output is correct
2 Correct 61 ms 91140 KB Output is correct
3 Correct 23 ms 88988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9052 KB Output is correct
2 Correct 58 ms 87520 KB Output is correct
3 Correct 21 ms 88944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 88924 KB Output is correct
2 Correct 21 ms 72292 KB Output is correct
3 Correct 19 ms 88776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 431 ms 43432 KB Output is correct
2 Correct 1512 ms 116100 KB Output is correct
3 Correct 554 ms 40564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 17648 KB Output is correct
2 Correct 553 ms 61760 KB Output is correct
3 Correct 403 ms 43252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 971 ms 66028 KB Output is correct
2 Correct 1955 ms 112144 KB Output is correct
3 Correct 527 ms 41112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 17888 KB Output is correct
2 Correct 1641 ms 114392 KB Output is correct
3 Correct 474 ms 40520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 505 ms 113028 KB Output is correct
2 Execution timed out 5039 ms 413700 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 458 ms 111952 KB Output is correct
2 Execution timed out 5050 ms 414528 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 92092 KB Output is correct
2 Execution timed out 5076 ms 455604 KB Time limit exceeded
3 Halted 0 ms 0 KB -