Submission #22929907
Source Code Expand
/**
* @FileName a.cpp
* @Author kanpurin
* @Created 2021.05.26 19:50:20
**/
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
template < class Monoid >
struct SegmentTree {
private:
using Func = std::function< Monoid(Monoid, Monoid) >;
Func F;
Monoid UNITY;
int n;
std::vector< Monoid > node;
int _binary_search(int a, int b, const std::function<bool(Monoid)> &f, Monoid &s, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = n;
if (r <= a || b <= l) return n;
if (a <= l && r <= b && !f(F(s,node[k]))) {
s = F(s,node[k]);
return n;
}
if (l == r - 1) {s = F(s,node[k]); return l;}
int ret = _binary_search(a, b, f, s, 2 * k + 1, l, (r - l) / 2 + l);
return ret != n ? ret : _binary_search(a, b, f, s, 2 * k + 2, (r - l) / 2 + l, r);
}
public:
SegmentTree() {}
SegmentTree(const std::vector< Monoid > &v, const Func f, const Monoid &unity) {
F = f;
UNITY = unity;
int sz = v.size();
n = 1;
while (n < sz) n <<= 1;
node.resize(n * 2 - 1, UNITY);
for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
build();
}
SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) {
F = f;
UNITY = unity;
n = 1;
while (n < m) n <<= 1;
node.resize(n * 2 - 1, UNITY);
if (val != UNITY) {
for (int i = 0; i < m; i++) node[i + n - 1] = val;
build();
}
}
void set(int k, const Monoid &x) {
node[n + k - 1] = x;
}
void build() {
for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
}
void update_query(int x, const Monoid &val) {
if (x >= n || x < 0) return;
x += n - 1;
node[x] = val;
while (x > 0) {
x = (x - 1) >> 1;
node[x] = F(node[2 * x + 1], node[2 * x + 2]);
}
}
Monoid get_query(int l, int r) {
Monoid L = UNITY, R = UNITY;
if (l < 0) l = 0;
if (r < 0) return UNITY;
if (l >= n) return UNITY;
if (r >= n) r = n;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) L = F(L, node[l++ - 1]);
if (r & 1) R = F(node[--r - 1], R);
}
return F(L, R);
}
int binary_search(int l, int r, const std::function<bool(Monoid)> &f) {
Monoid s = UNITY;
int ret = _binary_search(l,r,f,s);
return ret != n ? ret : -1;
}
Monoid operator[](int x) const {
return node[n + x - 1];
}
int size() {
return n;
}
void print() {
for (int i = 0; i < n; i++) {
std::cout << i << "\t: " << node[n + i - 1] << std::endl;
}
}
};
int main() {
int n;cin >> n;
vector<int> p(n);
vector<array<int,3>> q(n);
vector<ll> a_sum(n+1),b_sum(n+1),c_sum(n+1);
for (int i = 0; i < n; i++) {
int a;cin >> a; p[a-1] = i;
}
for (int i = 0; i < n; i++) {
cin >> q[i][0];
for (int j = 1; j < 3; j++) {
cin >> q[i][j];
q[i][j] = min(q[i][j],q[i][0]);
}
a_sum[i+1] = a_sum[i] + q[i][0];
b_sum[i+1] = b_sum[i] + q[i][1];
c_sum[i+1] = c_sum[i] + q[i][2];
}
SegmentTree<ll> dp(n,1LL<<60, [](ll a,ll b){return min(a,b);},1LL<<60);
for (int i = 0; i < n; i++) {
dp.update_query(p[i],min(dp.get_query(0,p[i])+a_sum[i],b_sum[i])-a_sum[i+1]);
}
ll ans = 1LL<<60;
for (int i = 0; i < n; i++) {
ans = min(ans,dp[p[i]]+a_sum[i+1]-c_sum[i+1]+c_sum[n]);
}
cout << ans << endl;
return 0;
}
Submission Info
| Submission Time |
|
| Task |
F - Insertion Sort |
| User |
kanpurin |
| Language |
C++ (GCC 9.2.1) |
| Score |
600 |
| Code Size |
3876 Byte |
| Status |
AC |
| Exec Time |
255 ms |
| Memory |
15100 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
600 / 600 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example0.txt, example1.txt, example2.txt, example3.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, example0.txt, example1.txt, example2.txt, example3.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
100 ms |
7796 KiB |
| 001.txt |
AC |
236 ms |
14424 KiB |
| 002.txt |
AC |
225 ms |
14028 KiB |
| 003.txt |
AC |
162 ms |
10036 KiB |
| 004.txt |
AC |
173 ms |
10204 KiB |
| 005.txt |
AC |
6 ms |
3744 KiB |
| 006.txt |
AC |
87 ms |
6440 KiB |
| 007.txt |
AC |
156 ms |
10104 KiB |
| 008.txt |
AC |
2 ms |
3528 KiB |
| 009.txt |
AC |
2 ms |
3440 KiB |
| 010.txt |
AC |
2 ms |
3608 KiB |
| 011.txt |
AC |
2 ms |
3408 KiB |
| 012.txt |
AC |
2 ms |
3632 KiB |
| 013.txt |
AC |
255 ms |
15100 KiB |
| 014.txt |
AC |
252 ms |
15076 KiB |
| 015.txt |
AC |
246 ms |
14952 KiB |
| 016.txt |
AC |
241 ms |
14892 KiB |
| 017.txt |
AC |
246 ms |
15068 KiB |
| 018.txt |
AC |
243 ms |
15080 KiB |
| 019.txt |
AC |
254 ms |
15084 KiB |
| 020.txt |
AC |
229 ms |
15048 KiB |
| 021.txt |
AC |
227 ms |
14952 KiB |
| 022.txt |
AC |
228 ms |
15072 KiB |
| example0.txt |
AC |
2 ms |
3596 KiB |
| example1.txt |
AC |
2 ms |
3476 KiB |
| example2.txt |
AC |
3 ms |
3616 KiB |
| example3.txt |
AC |
2 ms |
3496 KiB |