답안 #335770

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335770 2020-12-14T00:22:18 Z Agnimandur Discharging (NOI20_discharging) Java 11
11 / 100
184 ms 26740 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) {
        	if (ad.isEmpty() || l.m < ad.peekLast().m) {
        		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;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 8552 KB Output is correct
2 Incorrect 73 ms 8556 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 84 ms 8684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 84 ms 8684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 26460 KB Output is correct
2 Correct 181 ms 26660 KB Output is correct
3 Correct 184 ms 26584 KB Output is correct
4 Correct 178 ms 26720 KB Output is correct
5 Correct 179 ms 26612 KB Output is correct
6 Correct 180 ms 26576 KB Output is correct
7 Correct 181 ms 26592 KB Output is correct
8 Correct 178 ms 26740 KB Output is correct
9 Correct 179 ms 26496 KB Output is correct
10 Correct 178 ms 26592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 8552 KB Output is correct
2 Incorrect 73 ms 8556 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 8552 KB Output is correct
2 Incorrect 73 ms 8556 KB Output isn't correct
3 Halted 0 ms 0 KB -