题解 | #[NOIP2004]合并果子#
[NOIP2004]合并果子
https://ac.nowcoder.com/acm/problem/16663
采用数组模拟,思想类似于插入排序。
import java.util.Scanner;
public class NC16663 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []array = new int[n];
for(int i = 0;i<n;i++){
array[i] = sc.nextInt();
}
Arrays.sort(array);
int ans = 0;
for(int i = 0;i<n-1;i++){
array[i+1] = array[i]+array[i+1];
ans +=array[i+1];
int j = 2;
int z = 1;
while(i+j<n){
if(array[i+z]>array[i+j]){
int temp = array[i+z];
array[i+z] = array[i+j];
array[i+j] = temp;
z++;
j++;
}else{
j = 2;
break;
}
}
for(int h:array) System.out.print(h+" ");
System.out.println();
}
System.out.println(ans);
}
}