1. Title Description
Special test time limit: C / C ++ language 1000MS; Other Languages 3000MS memory limit: C / C ++ language 131072KB; Other Languages 655360KB Title Description: Small C do a particular server load testing, for a request requests in the queue, each request has a load value, in order to ensure stable server and requesting load queue must follow the law of diminishing after the first increment (only incremented, may be decremented only), such as [ 1,2,8,4,3] , [1,3,5] and [10] these laws are satisfied, and some are not satisfied, such as [1,2,2,1], [2,1,2] and [10, 10 ]. You are given a request queue, you can increase the load value of the request requires you to adjust the load value of requests in the queue, so that the array condition is satisfied. So that the final output queue to satisfy the smallest sum increasing condition. Input is input two lines, the first line is N ( 1≤n≤5000 ), represents the number of requests in the request queue. The second line has N numbers A1, A2 ... AN ( 1≤ai≤10 ^. 9 ). Ai is the i-th value of load request. Output outputs the sum of the minimum increase in sample input . 5 . 1. 4. 3 2. 5 sample output 6 prompt sample interpretation, then the queue is legitimate [ 1,4,5,6,5], and the minimum increase of 6
Code
Method 1 AC 72%
import java.util.Scanner; public class Main { private static int N;//正整数个数 public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); sc.nextLine(); int [] val = new int[N];//正整数 String[] str = sc.nextLine().split(" "); for (int i = 0; i < N; i++) { val[i] = Integer.valueOf(str[i]); } if(N==1){ System.out.println(0); return; } int left = 0; int right = N-1; int sum = 0; for (int i = 0; i < N-1; i++) { if(val[i]<val[i+1]){ left++; }else{ break; } } left++; for (int i = N-1; i > 0; i--) { if(val[i]<val[i-1]){ right--; }else{ break; } } right--; if(right< left){ if(right==left-1 && val[right]==val[left]){ sum = 1; }else{ sum = 0; } }else if(right==left){ sum += (Math.max(val[right-1], val[right+1])- val[right]+1); }else{ while(right>left){ if(val[left-1]< val[right+1]){ sum += (val[left-1] + 1 - val[left]); left++; }else if(val[left-1]> val[right+1]){ sum += (val[right+1] + 1 - val[right]); right--; }else{ sum += (val[left-1] + 1 - val[left]); left++; sum += (val[right+1] + 1 - val[right]); right--; } } if(right==left){ sum += (Math.max(val[right-1], val[right+1])- val[right]+1); } } System.out.print(sum); } }
Method Two greedy
import java.util.Arrays; import java.util.Scanner; public class A1 { private static int N;//正整数个数 public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); sc.nextLine(); long [] val = new long[N];//正整数 String[] str = sc.nextLine().split(" "); for (int i = 0; i < N; i++) { val[i] = Integer.valueOf(str[i]); } long sum = minSum(N,val); System.out.print(sum); } public static long minSum(int n ,long[] nums) { if(n == 1) return 0; if(n == 2 && nums[0] == nums[1]) return 1; if(n == 2 && nums[0] != nums[1]) return 0; long[] temp = Arrays.copyOf(nums, n); int i = 0; while(i < n-1 && nums[i] < nums[i+1]) i++; int j = n-1; while(j > 0 && nums[j] < nums[j-1]) j--; while(i < j) { if(nums[i] < nums[j]) { if(nums[i+1] < nums[i]+1) nums[i+1] = nums[i] + 1; i++; } else { if(nums[j-1] < nums[j]+1) nums[j-1] = nums[j] + 1; j--; } } return Arrays.stream(nums).sum() - Arrays.stream(temp).sum(); } }
URL:
https://www.nowcoder.com/discuss/220715?type=0&order=0&pos=2&page=1
https://www.nowcoder.com/discuss/220718?type=0&order=0&pos=5&page=1