Submission #489711

# Submission time Handle Problem Language Result Execution time Memory
489711 2021-11-24T03:14:52 Z cpp219 Sirni (COCI17_sirni) C++14
140 / 140
2142 ms 746908 KB
#include<bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e7 + 9;
const ll inf = 1e9 + 7;

vector<LL> g[N];
vector<ll> a;
ll n,x,near[N],lab[N/100];

ll Find(ll u){
    if (lab[u] < 0) return u;
    return lab[u] = Find(lab[u]);
}
void Union(ll r,ll s){
    r = Find(r); s = Find(s);
    if (r == s) return;
    if (lab[r] > lab[s]) swap(r,s);
    lab[r] += lab[s]; lab[s] = r;
}

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin>>n;
    for (ll i = 1;i <= n;i++) cin>>x,a.push_back(x);
    sort(a.begin(),a.end()); a.resize(unique(a.begin(),a.end()) - a.begin());
    ll prv = 0;
    for (ll i = 0;i < a.size();i++){
        for (ll j = prv + 1;j <= a[i];j++) near[j] = i;
        prv = a[i];
    }
    memset(lab,-1,sizeof(lab));
    for (ll i = 0;i < a.size() - 1;i++){
        ll now = a[i];
        for (ll j = now;j <= a.back();j += now){
            ll nxt = near[j];
            if (j == now) nxt = near[j + 1];
            ll val = a[nxt] % now;
            g[val].push_back({i,nxt});
            //cout<<now<<" "<<a[nxt]<<" "<<val<<"\n";
        }
    }
    ll ans = 0; //return 0;
    for (ll i = 0;i <= a.back();i++){
        for (auto j : g[i]){
            if (Find(j.fs) != Find(j.sc)) ans += i;
            Union(j.fs,j.sc);
        }
    }
    cout<<ans;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (ll i = 0;i < a.size();i++){
      |                   ~~^~~~~~~~~~
sirni.cpp:43:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (ll i = 0;i < a.size() - 1;i++){
      |                   ~~^~~~~~~~~~~~~~
sirni.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 176 ms 274788 KB Output is correct
2 Correct 274 ms 303780 KB Output is correct
3 Correct 181 ms 274904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 235628 KB Output is correct
2 Correct 1586 ms 669288 KB Output is correct
3 Correct 178 ms 275440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 274780 KB Output is correct
2 Correct 169 ms 274520 KB Output is correct
3 Correct 187 ms 274796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 184 ms 249492 KB Output is correct
2 Correct 313 ms 277948 KB Output is correct
3 Correct 220 ms 260948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 241308 KB Output is correct
2 Correct 215 ms 263084 KB Output is correct
3 Correct 201 ms 248232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 228 ms 261512 KB Output is correct
2 Correct 336 ms 295700 KB Output is correct
3 Correct 199 ms 259096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 239848 KB Output is correct
2 Correct 320 ms 296380 KB Output is correct
3 Correct 201 ms 260724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 260 ms 287820 KB Output is correct
2 Correct 1391 ms 633444 KB Output is correct
3 Correct 263 ms 292192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 258 ms 292400 KB Output is correct
2 Correct 2142 ms 746908 KB Output is correct
3 Correct 429 ms 348692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 276984 KB Output is correct
2 Correct 1811 ms 635076 KB Output is correct
3 Correct 209 ms 262880 KB Output is correct