公园里有 n 个花园,初始时每个花园里都没有种花,园丁将花园从 1 到 n 编号并计划在编号为 i 的花园里恰好种 Ai 朵花,他每天会选择一个区间 [L,R](1≤L≤R≤N)并在编号为 L 到 R 的花园里各种一朵花,那么园丁至少要花多少天才能完成计划?
数据范围:
, ![](https://www.nowcoder.com/equation?tex=0%20%5Cle%20A_i%20%5Cle%2010%5E4%20%5C)
第一行包含一个整数 n 。
第二行包含 n 个空格隔开的数 ai 到 an。
输出完成计划所需的最少天数。
5 4 1 8 2 5
14
5 1 1 1 1 1
1
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] ns = new int[n]; for (int i = 0; i < n; i++) { ns[i] = in.nextInt(); } int ans = 0; // 找到第一个大于0的数 int left = findLeft(ns, 0); while (left != -1) { // 从left开始,更新ns数组 ans += func(ns, left); // 找到第一个大于0的数 left = findLeft(ns, left); } System.out.println(ans); } private static int func(int[] ns, int left) { int min = ns[left]; int right = ns.length; // 第一次循环,维护min值和right for (int i = left + 1; i < ns.length; i++) { if (ns[i] == 0) { right = i; break; } if (ns[i] < min) { min = ns[i]; } } // 第二次循环,更新ns数组 for (int i = left; i < right; i++) { ns[i] -= min; } return min; } private static int findLeft(int[] ns, int pst) { // 找到left,即第一个大于0的数,若没有则返回-1 for (int i = pst; i < ns.length; i++) { if (ns[i] > 0) { return i; } } return -1; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @Author: coderjjp * @Date: 2020-05-11 12:40 * @Description: 种花 * @version: 1.0 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); String[] line2 = br.readLine().split(" "); int A[] = new int[N]; for (int i = 0; i < N; i++) A[i] = Integer.parseInt(line2[i]); int cur = 0; int ans = 0; while (cur < N && A[cur] == 0)//找到第一个不为0 cur++; while (cur < N){ //但下面这个循环耗时比较长 for (int j = cur; j < N && A[j] > 0; j++)//从cur开始向后种,能种的全种上 A[j]--; ans++;//天数加1 while (cur < N && A[cur] == 0)//种完一遍后从cur向后找到下一个不为0的待种花园 cur++; }//cur = N, 说明已全部种上,跳出循环,贪心法 System.out.println(ans); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @Author: coderjjp * @Date: 2020-05-11 13:42 * @Description: * @version: 1.0 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); String[] line2 = br.readLine().split(" "); int A[] = new int[N]; for (int i = 0; i < N; i++) A[i] = Integer.parseInt(line2[i]); int dp[] = new int[N]; dp[0] = A[0]; for (int i = 1; i < N; i++){ if (A[i] <= A[i-1]) dp[i] = dp[i-1]; else dp[i] = dp[i-1] + (A[i] - A[i-1]); } System.out.println(dp[N-1]); } }