here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.

Thursday, 23 October 2014

Gallantry

problem statement is here


#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
int a,b,c,d,ar[10004],br[10000],n,i,j,t;
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&ar[i]);
}
sort(ar,ar+t);
b=0;c=0;
for(i=0;i<t;i+=2){
br[b]=ar[i+1]-ar[i];
c+=br[b];
b++;
}
if(c==0){
printf("-1\n");
}else{
a=t/2;d=0;n=0;
sort(br,br+a);
for(i=0;i<a;i++){
if(c-br[i]>n+br[i]){
c-=br[i];
d++;
n+=br[i];
}else{
break;
}
}
printf("%d\n",d);
}
return 0;
}

1 comment:

  1. Hi, any person can say me what is the mistake in this code (Gallantry). I was try with the online judges but i have a "Wrong answer" everytime.


    #include
    #include

    using namespace std;

    int main()
    {
    int i;
    int j = 0;
    int cantidadJugadores;
    int vecesQuePasa = 0;

    cin >> cantidadJugadores;

    int *jugadores = new int[cantidadJugadores];
    int turnos = cantidadJugadores/2;
    int *german = new int[turnos];
    int *gianina = new int[turnos];
    int sumaGerman = 0;
    int sumaGianina = 0;

    for(i=0;i> jugadores[i];
    }

    //ordenar vector
    sort(jugadores, jugadores + cantidadJugadores); //orden ascendente

    for(i=cantidadJugadores;i>0;i= i-2)
    {
    german[j] = jugadores[i-1];
    sumaGerman = sumaGerman + german[j];
    gianina[j] = jugadores[i-2];
    sumaGianina = sumaGianina + gianina[j];
    j++;
    }

    if(sumaGerman > sumaGianina)
    {
    for(j=0;j sumaGianina)
    {
    vecesQuePasa += 1;
    }
    else
    {
    sumaGerman = sumaGerman + german[j] - gianina[j]; //suma el valor del intercambio de elementos
    sumaGianina = sumaGianina + gianina[j] - german[j];

    }
    }
    }
    else
    vecesQuePasa = -1;

    cout << vecesQuePasa << endl;

    return 0;
    }

    ReplyDelete