Submission #949263

# Submission time Handle Problem Language Result Execution time Memory
949263 2024-03-19T04:34:05 Z vjudge1 Sirni (COCI17_sirni) C++17
0 / 140
5000 ms 20376 KB
//#pragma GCC optimizer("03") ;
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
 
bool YES(bool f){ if(f) cout << "Yes\n" ; else cout << "No\n" ; return f ; }
void YES(){YES(1);}
void NO(){YES(0);}
void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
//#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ios ios_base::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0);
//#define int unsigned short
#define ld long double
#define pii pair <int , int>
#define all(x) x.begin() , x.end()
#define ff first
#define ss second
#define endl '\n'
 
const int N = 2e5 + 5 ;
const int inf = 1e9 ;
const int mod = 1e9 + 7 ;
const double eps = 1e-8 ;
 
template <class T>
bool chmax( T& x , const T& y ){
  bool f = 0 ;
  if ( x < y ) x = y , f = 1 ;
  return f ;
}
template <class T>
bool chmin( T &x , const T &y ){
  bool f = 0 ;
  if ( x > y ) x = y , f = 1 ;
  return f ;
}
 
//code
 
int n , ans , a[N] , p[N] , sz[N] ;
set <pii> st ;
vector <int> cmp[N] ;
set <int> c ;

int get ( int a ){
	if ( a == p[a] ) return a ;
	return p[a] = get ( p[a] ) ;
}

bool un ( int a , int b ){
	a = get(a) ;
	b = get(b) ;
	if ( a == b ) return 0 ;
	if ( sz[a] > sz[b] ) swap ( a , b ) ;
	sz[b] += sz[a] ; p[a] = b ;
	c.erase(a) ;
	for ( auto i : cmp[a] ) cmp[b].push_back(i) ;
	return 1 ;
}

void solve(){
	
	cin >> n ;
	for ( int i = 0 ; i < n ; i ++ ) cin >> a[i] ;
	sort ( a , a + n ) ;
	for ( int i = 0 ; i < n ; i ++ )
	 cmp[i].push_back(i) , c.insert(i) , st.insert({a[i],i}) , p[i] = i , sz[i] = 1 ;
	for ( int q = 0 ; ( 1 << q ) <= n ; q ++ ){
		vector <pii> b ;
		if ( c.size() == 1 ) break ;
		for ( int i = 0 ; i < n ; i ++ ){
			if ( p[i] != i ) continue ;
			vector <int> &cur = cmp[i] ;
			pii f = {inf,inf} , s ;
			for ( auto j : cur ) st.erase({a[j],j}) ;
			for ( auto j : cur ){
				for ( auto l : st ){
					if ( f.ff == inf ){
						f = { a[j] , j } ;
						s = l ;
					}
					else if ( min (f.ff%s.ff,s.ff%f.ff)>min(a[j]%l.ff,l.ff%a[j]) ){
						f = { a[j] , j } ;
						s = l ;
					}
					if ( min (f.ff%s.ff,s.ff%f.ff) == 0 ) break ;
				} break ;
				auto it = st.lower_bound({a[j],0}) ;
				auto mn = st.begin() ;
				if ( it != st.end() ){
					if ( f.ff == inf ){
						f = { a[j] , j } ;
						s = *it ;
					}
					else if ( min ( f.ff%s.ff , s.ff%f.ff ) > min ( a[j]%it->ff , it->ff%a[j] ) ){
						f = { a[j] , j } ;
						s = *it ;
					}
				}
				if ( mn != st.end() ){
					if ( f.ff == inf ){
						f = { a[j] , j } ;
						s = *mn ;
					}
					else if ( min ( f.ff%s.ff , s.ff%f.ff ) > min ( a[j]%mn->ff , mn->ff%a[j] ) ){
						f = { a[j] , j } ;
						s = *mn ;
					}
				}
			}
			b.push_back({f.ss,s.ss}) ;
			for ( auto j : cur ) st.insert({a[j],j}) ;
		}
		for ( auto i : b ){
			if ( un ( i.ff , i.ss ) ){
				ans += min ( a[i.ff]%a[i.ss] , a[i.ss]%a[i.ff] ) ;
				//cout << a[i.ff] << ' ' << a[i.ss] << endl ;
			}

		}
	}
	cout << ans ;
}

signed main(){
    ios ;
	int t = 1 ;
	//cin >> t ;
	while ( t -- ) solve() ;
}

Compilation message

sirni.cpp: In function 'void fopn(std::string)':
sirni.cpp:11:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:11:72: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 6892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5054 ms 20080 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3635 ms 9172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5031 ms 20376 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5025 ms 12488 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5012 ms 20160 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5021 ms 19916 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5040 ms 9304 KB Time limit exceeded
2 Halted 0 ms 0 KB -