General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
91261876 Practice:
shubho96
448C - 23 Java 8 Accepted 140 ms 0 KB 2020-08-29 09:42:12 2020-08-29 09:42:12
→ Source
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class PaintingFence {

    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int n  = sc.nextInt();
        int[] heights = sc.readArray(n);
        solve(n, heights);
    }

    private static void solve(int n, int[] heights) {

        int strokes = countStrokes(heights, 0, n - 1);
        System.out.println(strokes);

    }

    private static int countStrokes(int[] height, int start, int end){

        if(end < start)
            return 0;
        //System.out.println(start +", " + end);
        int minHeight = min(height, start, end);
        int segmentStrokes = minHeight;
        //update heights
        for(int i = start; i <= end; ++i){
            height[i] -= minHeight;
        }

        int currSegmentStart = end + 1;
        int currSegmentEnd = start;
        for(int i = start; i <= end; ++i){
            if(height[i] > 0){
                if(currSegmentStart == end + 1){
                    currSegmentStart = i;
                }
                currSegmentEnd = i;
            }
            else{
                segmentStrokes += countStrokes(height, currSegmentStart, currSegmentEnd);
                currSegmentStart = end + 1;
                currSegmentEnd = start;
            }
        }

        segmentStrokes += countStrokes(height, currSegmentStart, currSegmentEnd);

        segmentStrokes = Math.min(segmentStrokes, end - start + 1);
        return segmentStrokes;
    }

    private static int min(int[]arr, int start, int end){
        int min = Integer.MAX_VALUE;
        for(int i = start; i <= end; ++i)
            min = Math.min(min, arr[i]);

        return min;
    }
    private static class FastReader {
        private final BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; ++i)
                arr[i] = nextInt();

            return arr;
        }

        long[] readLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; ++i)
                arr[i] = nextLong();

            return arr;
        }

        double[] readDoubleArray(int n) {
            double[] arr = new double[n];
            for (int i = 0; i < n; ++i)
                arr[i] = nextDouble();
            return arr;
        }
    }
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details