Submission #335762

# Submission time Handle Problem Language Result Execution time Memory
335762 2020-12-13T22:28:13 Z Agnimandur Discharging (NOI20_discharging) Java 11
11 / 100
358 ms 72968 KB
import java.util.*;
import java.io.*;

public class Discharging {
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;
        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public int ni() throws IOException {
            int ret = 0;
            boolean flip = false;
            byte c = read();
            while (c < '0' || c > '9') {
                if (c == '-') flip = true;
                c = read();
            }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            return flip ? -ret : ret;
        }
        public long nl() throws IOException {
            long ret = 0;
            boolean flip = false;
            byte c = read();
            while (c < '0' || c > '9') {
                if (c == '-') flip = true;
                c = read();
            }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            return flip ? -ret : ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) {
                buffer[0] = -1;
            }
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead) {
                fillBuffer();
            }
            return buffer[bufferPointer++];
        }
    }

    public static void main(String[] args) throws IOException {
        Reader sc = new Reader();
        PrintStream out = new PrintStream(new BufferedOutputStream(System.out));
        
        int N = sc.ni();
        long[] nums = new long[N+1];
        for (int i = 1; i <= N; i++) {
            nums[i] = Math.max(nums[i-1],sc.nl());
        }
        
        //dp with convex hull trick
        long[] dp = new long[N+1];
        Hull cht = new Hull();
        for (int i = 1; i <= N; i++) {
            cht.add(new Line(nums[N-(i-1)],dp[i-1]));
            dp[i] = cht.getMin(i);
        }
        
        out.print(dp[N]);
        out.flush(); // remember to flush just once, at the very end of your program
    }

    //convex hull
    static class Hull {
        ArrayDeque<Line> ad;
        
        public Hull() {
            ad = new ArrayDeque<Line>();
        }

        public void add(Line l) {
            ad.addLast(l);
        }

        public long getMin(long x) {
            while (ad.size() >= 2) {
                Line f1 = ad.pollFirst();
                Line f2 = ad.peekFirst();
                if (!less(f1,f2,x)) {
                    ad.addFirst(f1);
                    break;
                }
            }
            return ad.peekFirst().eval(x);
        }

        public boolean less(Line f1, Line f2, long x) {
            return ((f2.b-f1.b) <= x*(f1.m-f2.m));
        }
    }

    //line
    static class Line {
        long m;
        long b;
        public Line(long m, long b) {
            this.m=m;
            this.b=b;
        }

        public long eval(long x) {
            return m*x+b;
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 72 ms 8556 KB Output is correct
2 Incorrect 75 ms 8556 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 85 ms 8684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 85 ms 8684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 348 ms 71352 KB Output is correct
2 Correct 346 ms 72708 KB Output is correct
3 Correct 358 ms 72740 KB Output is correct
4 Correct 346 ms 72712 KB Output is correct
5 Correct 342 ms 72664 KB Output is correct
6 Correct 343 ms 72828 KB Output is correct
7 Correct 345 ms 72588 KB Output is correct
8 Correct 347 ms 72968 KB Output is correct
9 Correct 353 ms 72640 KB Output is correct
10 Correct 341 ms 72844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 8556 KB Output is correct
2 Incorrect 75 ms 8556 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 72 ms 8556 KB Output is correct
2 Incorrect 75 ms 8556 KB Output isn't correct
3 Halted 0 ms 0 KB -