Submission #335777

# Submission time Handle Problem Language Result Execution time Memory
335777 2020-12-14T00:48:29 Z Agnimandur Discharging (NOI20_discharging) Java 11
100 / 100
580 ms 34420 KB
import java.util.*;
import java.io.*;
import java.math.BigInteger;

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) {
        		while (ad.size() >= 2) {
        			Line l1 = ad.pollLast();
                    Line l2 = ad.peekLast();
                    if (good(l2,l1,l)) {
                    	ad.addLast(l1);
                    	break;
                    }
        		}
        		ad.addLast(l);
        	}
        }
        
        public boolean good(Line l1, Line l2, Line l3) {
        	double d12 = (l2.b-l1.b+0.0)/(l1.m-l2.m);
        	double d23 = (l3.b-l2.b+0.0)/(l2.m-l3.m);
        	if (Math.abs(d12-d23) < 1e-10) {
        		BigInteger b12 = (new BigInteger(Long.toString(l2.b-l1.b))).multiply(new BigInteger(Long.toString(l2.m-l3.m)));
        		BigInteger b23 = (new BigInteger(Long.toString(l3.b-l2.b))).multiply(new BigInteger(Long.toString(l1.m-l2.m)));
        		if (b12.compareTo(b23) < 0)
        			return true;
        		else
        			return false;
        	} else {
        		return (d12 < d23);
        	}
        }

        public long getMin(long x) {
            while (ad.size() >= 2) {
                Line f1 = ad.pollFirst();
                Line f2 = ad.peekFirst();
                if ((f2.b-f1.b) > x*(f1.m-f2.m)) {
                    ad.addFirst(f1);
                    break;
                }
            }
            return ad.peekFirst().eval(x);
        }
    }

    //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 73 ms 8556 KB Output is correct
2 Correct 72 ms 8556 KB Output is correct
3 Correct 79 ms 8556 KB Output is correct
4 Correct 74 ms 8556 KB Output is correct
5 Correct 71 ms 8560 KB Output is correct
6 Correct 73 ms 8684 KB Output is correct
7 Correct 70 ms 8556 KB Output is correct
8 Correct 73 ms 8428 KB Output is correct
9 Correct 72 ms 8468 KB Output is correct
10 Correct 87 ms 8700 KB Output is correct
11 Correct 77 ms 8684 KB Output is correct
12 Correct 73 ms 8556 KB Output is correct
13 Correct 70 ms 8556 KB Output is correct
14 Correct 72 ms 8556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 8812 KB Output is correct
2 Correct 101 ms 8868 KB Output is correct
3 Correct 86 ms 8704 KB Output is correct
4 Correct 83 ms 8700 KB Output is correct
5 Correct 84 ms 8684 KB Output is correct
6 Correct 86 ms 8684 KB Output is correct
7 Correct 88 ms 8712 KB Output is correct
8 Correct 88 ms 8812 KB Output is correct
9 Correct 88 ms 8684 KB Output is correct
10 Correct 90 ms 8684 KB Output is correct
11 Correct 87 ms 8812 KB Output is correct
12 Correct 82 ms 8680 KB Output is correct
13 Correct 88 ms 8680 KB Output is correct
14 Correct 86 ms 8580 KB Output is correct
15 Correct 86 ms 8684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 8812 KB Output is correct
2 Correct 101 ms 8868 KB Output is correct
3 Correct 86 ms 8704 KB Output is correct
4 Correct 83 ms 8700 KB Output is correct
5 Correct 84 ms 8684 KB Output is correct
6 Correct 86 ms 8684 KB Output is correct
7 Correct 88 ms 8712 KB Output is correct
8 Correct 88 ms 8812 KB Output is correct
9 Correct 88 ms 8684 KB Output is correct
10 Correct 90 ms 8684 KB Output is correct
11 Correct 87 ms 8812 KB Output is correct
12 Correct 82 ms 8680 KB Output is correct
13 Correct 88 ms 8680 KB Output is correct
14 Correct 86 ms 8580 KB Output is correct
15 Correct 86 ms 8684 KB Output is correct
16 Correct 548 ms 34112 KB Output is correct
17 Correct 498 ms 32988 KB Output is correct
18 Correct 464 ms 34024 KB Output is correct
19 Correct 550 ms 34000 KB Output is correct
20 Correct 538 ms 34124 KB Output is correct
21 Correct 520 ms 33480 KB Output is correct
22 Correct 558 ms 34368 KB Output is correct
23 Correct 548 ms 34116 KB Output is correct
24 Correct 580 ms 34316 KB Output is correct
25 Correct 525 ms 33964 KB Output is correct
26 Correct 536 ms 34420 KB Output is correct
27 Correct 557 ms 34268 KB Output is correct
28 Correct 532 ms 33412 KB Output is correct
29 Correct 538 ms 33952 KB Output is correct
30 Correct 554 ms 33944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 26336 KB Output is correct
2 Correct 179 ms 26208 KB Output is correct
3 Correct 176 ms 26192 KB Output is correct
4 Correct 179 ms 26340 KB Output is correct
5 Correct 177 ms 26464 KB Output is correct
6 Correct 177 ms 26464 KB Output is correct
7 Correct 176 ms 26336 KB Output is correct
8 Correct 175 ms 26308 KB Output is correct
9 Correct 179 ms 26464 KB Output is correct
10 Correct 175 ms 26464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 8556 KB Output is correct
2 Correct 72 ms 8556 KB Output is correct
3 Correct 79 ms 8556 KB Output is correct
4 Correct 74 ms 8556 KB Output is correct
5 Correct 71 ms 8560 KB Output is correct
6 Correct 73 ms 8684 KB Output is correct
7 Correct 70 ms 8556 KB Output is correct
8 Correct 73 ms 8428 KB Output is correct
9 Correct 72 ms 8468 KB Output is correct
10 Correct 87 ms 8700 KB Output is correct
11 Correct 77 ms 8684 KB Output is correct
12 Correct 73 ms 8556 KB Output is correct
13 Correct 70 ms 8556 KB Output is correct
14 Correct 72 ms 8556 KB Output is correct
15 Correct 90 ms 8812 KB Output is correct
16 Correct 101 ms 8868 KB Output is correct
17 Correct 86 ms 8704 KB Output is correct
18 Correct 83 ms 8700 KB Output is correct
19 Correct 84 ms 8684 KB Output is correct
20 Correct 86 ms 8684 KB Output is correct
21 Correct 88 ms 8712 KB Output is correct
22 Correct 88 ms 8812 KB Output is correct
23 Correct 88 ms 8684 KB Output is correct
24 Correct 90 ms 8684 KB Output is correct
25 Correct 87 ms 8812 KB Output is correct
26 Correct 82 ms 8680 KB Output is correct
27 Correct 88 ms 8680 KB Output is correct
28 Correct 86 ms 8580 KB Output is correct
29 Correct 86 ms 8684 KB Output is correct
30 Correct 78 ms 8680 KB Output is correct
31 Correct 79 ms 8556 KB Output is correct
32 Correct 82 ms 8668 KB Output is correct
33 Correct 76 ms 8556 KB Output is correct
34 Correct 80 ms 8400 KB Output is correct
35 Correct 78 ms 8556 KB Output is correct
36 Correct 76 ms 8684 KB Output is correct
37 Correct 78 ms 8664 KB Output is correct
38 Correct 76 ms 8556 KB Output is correct
39 Correct 78 ms 8668 KB Output is correct
40 Correct 80 ms 8556 KB Output is correct
41 Correct 78 ms 8556 KB Output is correct
42 Correct 76 ms 8684 KB Output is correct
43 Correct 87 ms 8812 KB Output is correct
44 Correct 81 ms 8556 KB Output is correct
45 Correct 75 ms 8556 KB Output is correct
46 Correct 76 ms 8684 KB Output is correct
47 Correct 80 ms 8684 KB Output is correct
48 Correct 78 ms 8704 KB Output is correct
49 Correct 77 ms 8448 KB Output is correct
50 Correct 86 ms 8576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 8556 KB Output is correct
2 Correct 72 ms 8556 KB Output is correct
3 Correct 79 ms 8556 KB Output is correct
4 Correct 74 ms 8556 KB Output is correct
5 Correct 71 ms 8560 KB Output is correct
6 Correct 73 ms 8684 KB Output is correct
7 Correct 70 ms 8556 KB Output is correct
8 Correct 73 ms 8428 KB Output is correct
9 Correct 72 ms 8468 KB Output is correct
10 Correct 87 ms 8700 KB Output is correct
11 Correct 77 ms 8684 KB Output is correct
12 Correct 73 ms 8556 KB Output is correct
13 Correct 70 ms 8556 KB Output is correct
14 Correct 72 ms 8556 KB Output is correct
15 Correct 90 ms 8812 KB Output is correct
16 Correct 101 ms 8868 KB Output is correct
17 Correct 86 ms 8704 KB Output is correct
18 Correct 83 ms 8700 KB Output is correct
19 Correct 84 ms 8684 KB Output is correct
20 Correct 86 ms 8684 KB Output is correct
21 Correct 88 ms 8712 KB Output is correct
22 Correct 88 ms 8812 KB Output is correct
23 Correct 88 ms 8684 KB Output is correct
24 Correct 90 ms 8684 KB Output is correct
25 Correct 87 ms 8812 KB Output is correct
26 Correct 82 ms 8680 KB Output is correct
27 Correct 88 ms 8680 KB Output is correct
28 Correct 86 ms 8580 KB Output is correct
29 Correct 86 ms 8684 KB Output is correct
30 Correct 548 ms 34112 KB Output is correct
31 Correct 498 ms 32988 KB Output is correct
32 Correct 464 ms 34024 KB Output is correct
33 Correct 550 ms 34000 KB Output is correct
34 Correct 538 ms 34124 KB Output is correct
35 Correct 520 ms 33480 KB Output is correct
36 Correct 558 ms 34368 KB Output is correct
37 Correct 548 ms 34116 KB Output is correct
38 Correct 580 ms 34316 KB Output is correct
39 Correct 525 ms 33964 KB Output is correct
40 Correct 536 ms 34420 KB Output is correct
41 Correct 557 ms 34268 KB Output is correct
42 Correct 532 ms 33412 KB Output is correct
43 Correct 538 ms 33952 KB Output is correct
44 Correct 554 ms 33944 KB Output is correct
45 Correct 177 ms 26336 KB Output is correct
46 Correct 179 ms 26208 KB Output is correct
47 Correct 176 ms 26192 KB Output is correct
48 Correct 179 ms 26340 KB Output is correct
49 Correct 177 ms 26464 KB Output is correct
50 Correct 177 ms 26464 KB Output is correct
51 Correct 176 ms 26336 KB Output is correct
52 Correct 175 ms 26308 KB Output is correct
53 Correct 179 ms 26464 KB Output is correct
54 Correct 175 ms 26464 KB Output is correct
55 Correct 78 ms 8680 KB Output is correct
56 Correct 79 ms 8556 KB Output is correct
57 Correct 82 ms 8668 KB Output is correct
58 Correct 76 ms 8556 KB Output is correct
59 Correct 80 ms 8400 KB Output is correct
60 Correct 78 ms 8556 KB Output is correct
61 Correct 76 ms 8684 KB Output is correct
62 Correct 78 ms 8664 KB Output is correct
63 Correct 76 ms 8556 KB Output is correct
64 Correct 78 ms 8668 KB Output is correct
65 Correct 80 ms 8556 KB Output is correct
66 Correct 78 ms 8556 KB Output is correct
67 Correct 76 ms 8684 KB Output is correct
68 Correct 87 ms 8812 KB Output is correct
69 Correct 81 ms 8556 KB Output is correct
70 Correct 75 ms 8556 KB Output is correct
71 Correct 76 ms 8684 KB Output is correct
72 Correct 80 ms 8684 KB Output is correct
73 Correct 78 ms 8704 KB Output is correct
74 Correct 77 ms 8448 KB Output is correct
75 Correct 86 ms 8576 KB Output is correct
76 Correct 243 ms 32876 KB Output is correct
77 Correct 252 ms 33080 KB Output is correct
78 Correct 244 ms 32988 KB Output is correct
79 Correct 244 ms 32872 KB Output is correct
80 Correct 248 ms 32808 KB Output is correct
81 Correct 251 ms 32968 KB Output is correct
82 Correct 244 ms 32872 KB Output is correct
83 Correct 239 ms 32872 KB Output is correct
84 Correct 249 ms 32720 KB Output is correct
85 Correct 252 ms 32916 KB Output is correct
86 Correct 246 ms 33000 KB Output is correct
87 Correct 245 ms 32744 KB Output is correct
88 Correct 244 ms 32704 KB Output is correct
89 Correct 241 ms 32744 KB Output is correct
90 Correct 243 ms 32852 KB Output is correct
91 Correct 246 ms 32856 KB Output is correct
92 Correct 529 ms 34072 KB Output is correct
93 Correct 563 ms 33992 KB Output is correct
94 Correct 245 ms 32856 KB Output is correct
95 Correct 244 ms 32908 KB Output is correct
96 Correct 247 ms 32976 KB Output is correct
97 Correct 247 ms 32960 KB Output is correct
98 Correct 242 ms 32868 KB Output is correct
99 Correct 239 ms 32872 KB Output is correct
100 Correct 247 ms 32876 KB Output is correct