ACM (UVa) : 591


Add up all h
5 + 2 + 4 + 1 + 7 + 5 = 24
divide the result with n
24 / 6 = 4
now, take each individual h and compare it with quotient.

If (hi > quotient)
count = count + (hi – quotient)
where, i denotes 1, 2, 3 … i.e. every individual h.
output: count

Critical input:
10
10 20 30 40 50 60 70 80 90 100
5
6 8 5 4 2
3
20 30 80
0

Critical output:
Set #1
The minimum number of moves is 125.

Set #2
The minimum number of moves is 4.

Set #3
The minimum number of moves is 37.

Advertisements

One thought on “ACM (UVa) : 591

  1. #include <stdio.h>
    
    int main()
    {
    
        long n, total, count, avg, a [105], set = 0, i; 
        
    	while(scanf("%d", &n)){
            
    		total = count = 0;
            
    		if(n == 0)
                return 0;
            
    		for(i = 0; i < n; i++) {
                scanf("%ld", & a [i]);
                total += a [i];
            }
            
    		avg = total / n;
            
    		for(i = 0; i < n; i++){
                if( a[i] > avg)
                    count += (a [i] - avg); 
            }
    
            printf("Set #%d\n", ++set);
    		printf("The minimum number of moves is %d.\n\n", count);
        }
        return 0;
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s