Submission #335770

#TimeUsernameProblemLanguageResultExecution timeMemory
335770AgnimandurDischarging (NOI20_discharging)Java
11 / 100
184 ms26740 KiB
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; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...