ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

why carsh?
Posted by liulian 2 Nov 2007 22:25
#include<stdio.h>
#include<stdlib.h>
int split(int st[],int low,int high){                                         /*____________________*/
           int    k,i=low;
           int    temp;
           int    x=st[low];
                  for(k=low+1;k<=high;k++)
                            if (st[k]<=x){
                                        i=i+1;
                                        if(i!=k){
                                                 temp=st[i];
                                                 st[i]=st[k];                                                /*___sort____*/
                                                 st[k]=temp;
                                        }
                            }
                 {temp=st[low];st[low]=st[i];st[i]=temp;}
         return i;
}
void sort(int st[],int low,int high){
         int   k;
                 if(low<high){
                              k=split(st,low,high);
                              sort(st,low,k-1);
                              sort(st,k+1,high);
                 }
}                                                                                       /*___________________*/
int main()
{    int  i,j,stone;                   /*the number of stone*/
     int weight[20]={0};
     int sum,average;
     int min,max,value,temp=0;
                  while(scanf("%d",&stone)!=EOF){
                                   for(sum=0,i=1;i<=stone;i++){
                                                 scanf("%d",&weight[i]);
                                                 sum=sum+weight[i];
                                   }
                                   average=sum/2;
                                   sort(weight,1,stone);
                                   for(max=0,i=1;i<=stone;i++){
                                                 max=max+weight[i];
                                                 if(max>=average) break;
                                   }
                                   for(min=0,j=i+1;j<=stone;j++)
                                                min=min+weight[j];
                                   if(min>max){temp=max;max=min;min=temp;}
                                   for(value=sum,i=1;(max-min)>=0;i++){
                                                    if((max-min)<value) value=max-min;
                                                    max-=weight[i];
                                                    min+=weight[i];
                                   }
                                   printf("%d\n",value);
              }
     return 0;
}

Edited by author 02.11.2007 22:26

Edited by author 02.11.2007 22:26