Submission #147703

# Submission time Handle Problem Language Result Execution time Memory
147703 2019-08-30T12:41:34 Z Ruxandra985 Sirni (COCI17_sirni) C++14
42 / 140
2462 ms 124020 KB
#include <cstdio>
#include <algorithm>
#define DIM 100010
using namespace std;
int v[DIM];
struct muchie {
    int cost , x , y;
} m[1800000];
int tt[DIM];
int biggest[10000010];
int cmp (muchie a , muchie b){
    if (a.cost != b.cost)
        return a.cost<b.cost;
    if (a.x != b.x)
        return a.x<b.x;
    return a.y < b.y;
}
int fth ( int x ){
    int aux;
    int init = x;
    while (tt[x]>=0){
        x = tt[x];
    }
    while (tt[init]>=0){
        aux = tt[init];
        tt[init] = x;
        init = aux;
    }

    return x;
}
int main()
{
   // freopen ("a.in" , "r" , stdin);
   // freopen ("a.out" , "w" , stdout);
    int n,i,elem,p,mch,j,x,y,tx,ty;
    long long sol=0;
    scanf ("%d",&n);
    for (i=1;i<=n;i++){
        scanf ("%d",&v[i]);
        tt[i] = -1;
    }
    sort (v+1,v+n+1);
    elem = 0;
    for (i=1;i<=n;i++){
        if (i==1 || v[i]!=v[i-1])
            v[++elem] = v[i];
    }
    n = elem; /// MA IMPUSC NU POT SA CRED CA AM PUS ASTA DUPA FOR
    p=n;
    for (i=v[n];i;i--){
        if (p && v[p] == i){
            biggest[i] = p;
            p--;
        }
        else biggest[i] = biggest[i+1];
    }
    mch=0;
    for (i=1;i<=n;i++){
        if (i!=n && v[i+1]/v[i] == 1){
            mch++;
            m[mch].cost = v[i+1] % v[i];
            m[mch].x = i;
            m[mch].y = i+1;
            //printf ("%d %d %d\n",v[i],v[i+1],v[i+1] % v[i]);
        }
        for (j=2*v[i];j<=v[n];j+=v[i]){
            /// cea mai mica val mai mare decat j
            x = biggest[j];
            if (j/v[i] == v[x]/v[i]){ /// add muchie
                mch++;
                m[mch].cost = v[x] % v[i];
                m[mch].x = i;
                m[mch].y = x;
                //printf ("%d %d %d\n",v[i],v[x],v[x] % v[i]);
            }
        }
    }
    sort (m+1,m + mch + 1 , cmp);
    for (i=1;i<=mch;i++){
        x = m[i].x;
        y = m[i].y;
        tx = fth(x);
        ty = fth(y);
        if (tx!=ty){
            sol+=m[i].cost;
            //printf ("%d ",m[i].cost);
            if (tt[tx] < tt[ty]){
                tt[tx]+=tt[ty];
                tt[ty] = tx;
            }
            else {
                tt[ty]+=tt[tx];
                tt[tx] = ty;
            }
        }
    }
    printf ("%lld",sol);
    return 0;
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:38:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d",&n);
     ~~~~~~^~~~~~~~~
sirni.cpp:40:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d",&v[i]);
         ~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 54 ms 39416 KB Output is correct
2 Correct 163 ms 41584 KB Output is correct
3 Correct 55 ms 39420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 753 ms 40412 KB Output is correct
3 Correct 57 ms 39544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 39544 KB Output is correct
2 Correct 55 ms 39544 KB Output is correct
3 Correct 55 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 277 ms 17016 KB Output is correct
2 Runtime error 202 ms 52540 KB Execution killed with signal 8 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 39 ms 6008 KB Output is correct
2 Runtime error 166 ms 52676 KB Execution killed with signal 8 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 113 ms 52600 KB Execution killed with signal 8 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 89 ms 5576 KB Output is correct
2 Runtime error 141 ms 52588 KB Execution killed with signal 8 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 403 ms 54464 KB Output is correct
2 Runtime error 631 ms 124000 KB Execution killed with signal 8 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 381 ms 54392 KB Output is correct
2 Runtime error 2462 ms 124020 KB Execution killed with signal 8 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 98 ms 41720 KB Output is correct
2 Runtime error 1669 ms 123980 KB Execution killed with signal 8 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -