Submission #335747

# Submission time Handle Problem Language Result Execution time Memory
335747 2020-12-13T20:15:43 Z nafis_shifat Discharging (NOI20_discharging) C++14
100 / 100
446 ms 25868 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e6+5;
const ll inf=1e18;


vector<ll> m,b;

ll f(int i, ll x) { return m[i] * x + b[i]; }


bool bad(int f1,int f2,int f3) {
	return __int128(b[f3] - b[f1]) * (m[f1] - m[f2]) <= __int128(b[f2] - b[f1]) * (m[f1] - m[f3]);
}

void add(ll m_, ll b_) {
	m.push_back(m_);
	b.push_back(b_);

	int sz = m.size();

	while(sz >=3 && bad(sz-3,sz-2,sz-1)) {
		m.erase(m.end() - 2);
		b.erase(b.end() - 2);
		sz--;
	}
}

ll query(ll x) {
	ll ans = inf;

	int lo = 0;
	int hi = m.size() - 1;

	while(lo <= hi) {
		int mid1 = lo + (hi - lo) / 3;
		int mid2 = hi - (hi - hi) / 3;

		ll v1 = f(mid1,x);
		ll v2 = f(mid2,x);

		if(v1 <= v2) {
			ans = min(ans,v1);
			hi = mid2 - 1;
		} else {
			ans = min(ans,v2);
			lo = mid1 + 1;
		}
	}

	return ans;
}



int main() {
	int n;
	cin>>n;

	ll t[n+1];


	for(int i = 1; i <= n; i++) cin>>t[i];


	ll dp[n+1];

    dp[0] = 0;

    add(0,0);
    ll cm = 0;
    int lp = 1;


    for(int i = 1; i <= n; i++) {
    	if(t[i] >= cm) {
    		cm = t[i];
    		for(; lp < i; lp++) {
    			add(-lp,dp[lp]);
    		}
    	}
    	dp[i] = query(cm) + cm * n;
    	
    }

    cout<<dp[n]<<endl;
	
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 345 ms 16108 KB Output is correct
17 Correct 396 ms 21996 KB Output is correct
18 Correct 322 ms 20332 KB Output is correct
19 Correct 395 ms 21740 KB Output is correct
20 Correct 368 ms 21732 KB Output is correct
21 Correct 397 ms 21740 KB Output is correct
22 Correct 361 ms 21484 KB Output is correct
23 Correct 402 ms 22508 KB Output is correct
24 Correct 386 ms 21868 KB Output is correct
25 Correct 405 ms 22380 KB Output is correct
26 Correct 421 ms 22636 KB Output is correct
27 Correct 352 ms 21100 KB Output is correct
28 Correct 385 ms 22124 KB Output is correct
29 Correct 413 ms 22088 KB Output is correct
30 Correct 412 ms 22380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 415 ms 15980 KB Output is correct
2 Correct 442 ms 25708 KB Output is correct
3 Correct 425 ms 25748 KB Output is correct
4 Correct 430 ms 25708 KB Output is correct
5 Correct 446 ms 25580 KB Output is correct
6 Correct 419 ms 25868 KB Output is correct
7 Correct 424 ms 25580 KB Output is correct
8 Correct 421 ms 25708 KB Output is correct
9 Correct 416 ms 25596 KB Output is correct
10 Correct 423 ms 25836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 2 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 345 ms 16108 KB Output is correct
31 Correct 396 ms 21996 KB Output is correct
32 Correct 322 ms 20332 KB Output is correct
33 Correct 395 ms 21740 KB Output is correct
34 Correct 368 ms 21732 KB Output is correct
35 Correct 397 ms 21740 KB Output is correct
36 Correct 361 ms 21484 KB Output is correct
37 Correct 402 ms 22508 KB Output is correct
38 Correct 386 ms 21868 KB Output is correct
39 Correct 405 ms 22380 KB Output is correct
40 Correct 421 ms 22636 KB Output is correct
41 Correct 352 ms 21100 KB Output is correct
42 Correct 385 ms 22124 KB Output is correct
43 Correct 413 ms 22088 KB Output is correct
44 Correct 412 ms 22380 KB Output is correct
45 Correct 415 ms 15980 KB Output is correct
46 Correct 442 ms 25708 KB Output is correct
47 Correct 425 ms 25748 KB Output is correct
48 Correct 430 ms 25708 KB Output is correct
49 Correct 446 ms 25580 KB Output is correct
50 Correct 419 ms 25868 KB Output is correct
51 Correct 424 ms 25580 KB Output is correct
52 Correct 421 ms 25708 KB Output is correct
53 Correct 416 ms 25596 KB Output is correct
54 Correct 423 ms 25836 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 1 ms 364 KB Output is correct
57 Correct 1 ms 364 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1 ms 364 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 1 ms 364 KB Output is correct
67 Correct 1 ms 364 KB Output is correct
68 Correct 1 ms 364 KB Output is correct
69 Correct 1 ms 364 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 2 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 1 ms 364 KB Output is correct
75 Correct 1 ms 364 KB Output is correct
76 Correct 340 ms 21996 KB Output is correct
77 Correct 340 ms 21996 KB Output is correct
78 Correct 323 ms 21356 KB Output is correct
79 Correct 333 ms 21712 KB Output is correct
80 Correct 351 ms 22380 KB Output is correct
81 Correct 326 ms 21612 KB Output is correct
82 Correct 326 ms 21612 KB Output is correct
83 Correct 340 ms 22136 KB Output is correct
84 Correct 339 ms 21868 KB Output is correct
85 Correct 346 ms 22124 KB Output is correct
86 Correct 322 ms 21484 KB Output is correct
87 Correct 319 ms 21356 KB Output is correct
88 Correct 335 ms 21740 KB Output is correct
89 Correct 331 ms 21612 KB Output is correct
90 Correct 326 ms 21420 KB Output is correct
91 Correct 315 ms 21356 KB Output is correct
92 Correct 413 ms 22508 KB Output is correct
93 Correct 386 ms 21740 KB Output is correct
94 Correct 304 ms 20844 KB Output is correct
95 Correct 355 ms 22320 KB Output is correct
96 Correct 320 ms 21228 KB Output is correct
97 Correct 329 ms 21740 KB Output is correct
98 Correct 337 ms 21740 KB Output is correct
99 Correct 327 ms 21716 KB Output is correct
100 Correct 324 ms 21612 KB Output is correct