Submission #147596

# Submission time Handle Problem Language Result Execution time Memory
147596 2019-08-30T08:44:46 Z Ruxandra985 Sirni (COCI17_sirni) C++14
0 / 140
5000 ms 42104 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){
    return a.cost<=b.cost;
}
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];
    }
    p=n;
    for (i=v[n];i;i--){
        if (p && v[p] == i){
            biggest[i] = p;
            p--;
        }
        else biggest[i] = biggest[i+1];
    }
    n = elem; /// distincte
    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;
            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:34:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d",&n);
     ~~~~~~^~~~~~~~~
sirni.cpp:36: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 53 ms 39416 KB Output is correct
2 Execution timed out 5028 ms 40136 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5086 ms 764 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 39544 KB Output is correct
2 Incorrect 53 ms 39480 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 586 ms 6776 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5091 ms 4856 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 599 ms 6740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 157 ms 2040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5061 ms 42024 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 212 ms 42104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 68 ms 39888 KB Output isn't correct
2 Halted 0 ms 0 KB -