QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#76805 | #5507. Investors | Forested | AC ✓ | 6958ms | 284968kb | C++17 | 6.3kb | 2023-02-12 11:30:35 | 2023-02-12 11:30:38 |
Judging History
answer
#ifndef LOCAL
#define FAST_IO
#endif
// ============
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32)(n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;
template <typename T>
using Vec = vector<T>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T>
Vec<tuple<i32, i32, T>> runlength(const Vec<T> &a) {
if (a.empty()) {
return Vec<tuple<i32, i32, T>>();
}
Vec<tuple<i32, i32, T>> ret;
i32 prv = 0;
REP(i, 1, a.size()) {
if (a[i - 1] != a[i]) {
ret.emplace_back(prv, i, a[i - 1]);
prv = i;
}
}
ret.emplace_back(prv, (i32)a.size(), a.back());
return ret;
}
#ifdef INT128
using u128 = __uint128_t;
using i128 = __int128_t;
istream &operator>>(istream &is, i128 &x) {
i64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, i128 x) {
os << (i64)x;
return os;
}
istream &operator>>(istream &is, u128 &x) {
u64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, u128 x) {
os << (u64)x;
return os;
}
#endif
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
SetUpIO() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << fixed << setprecision(15);
}
} set_up_io;
// ============
#ifdef DEBUGF
#else
#define DBG(x) (void)0
#endif
// ============
#include <cassert>
#include <utility>
#include <functional>
template <typename F, typename I, typename Comp = std::less<>>
auto golden_search(const F &f, I mn, I mx, Comp comp = Comp()) -> std::pair<I, decltype(f(mn))> {
using V = decltype(f(mn));
assert(mn <= mx);
I x0, x1, x2;
{
x0 = mn - 1;
I s = 1, t = 1;
while (x0 + s + t <= mx) {
I u = s + t;
s = t;
t = u;
}
x1 = x0 + s + t;
x2 = x0 + s;
}
V fx2 = f(x2);
V fx3 = 0;
while (2 * x2 != x0 + x1) {
I x3 = x0 + x1 - x2;
if (x3 > mx) {
x1 = x0;
x0 = x3;
} else {
fx3 = f(x3);
if (comp(fx2, fx3)) {
x1 = x0;
x0 = x3;
} else {
x0 = x2;
x2 = x3;
fx2 = fx3;
}
}
}
return std::pair<I, V>(x2, fx2);
}
// ============
// ============
#include <algorithm>
#include <vector>
template <typename T>
class CoordinateCompression {
std::vector<T> data;
int size_sum() {
return 0;
}
template <typename... Tail>
int size_sum(const std::vector<T> &head, const Tail &...tail) {
return (int) head.size() + size_sum(tail...);
}
void push() {}
template <typename... Tail>
void push(const std::vector<T> &head, const Tail &...tail) {
for (const T &ele : head) {
data.emplace_back(ele);
}
push(tail...);
}
void compress() {}
template <typename... Tail>
void compress(std::vector<T> &head, Tail &...tail) {
for (T &ele : head) {
ele = (T) (std::lower_bound(data.begin(), data.end(), ele) - data.begin());
}
compress(tail...);
}
public:
template <typename... V>
CoordinateCompression(V &...v) {
data.reserve(size_sum(v...));
push(v...);
std::sort(data.begin(), data.end());
data.erase(std::unique(data.begin(), data.end()), data.end());
compress(v...);
}
const T &operator[](const T &ele) const {
return data[ele];
}
int size() const {
return data.size();
}
bool contains(const T &ele) const {
auto it = std::lower_bound(data.begin(), data.end(), ele);
return it != data.end() && *it == ele;
}
T cc(const T &ele) const {
return (T) (std::lower_bound(data.begin(), data.end(), ele) - data.begin());
}
};
// ============
using usize = i32;
std::vector<std::vector<usize>> calc_inv(std::vector<usize> p) {
usize n = p.size();
std::vector<std::vector<usize>> sum(n + 1, std::vector<usize>(n + 1, 0));
for (usize i = 0; i < n; ++i) {
sum[i + 1] = sum[i];
for (usize j = p[i]; j < n; ++j) {
++sum[i + 1][j + 1];
}
}
std::vector<std::vector<usize>> inv(n, std::vector<usize>(n + 1, 0));
for (usize i = 0; i < n; ++i) {
usize s = 0;
for (usize j = i; j < n; ++j) {
s += sum[j][n] - sum[i][n] - sum[j][p[j] + 1] + sum[i][p[j] + 1];
inv[i][j + 1] = s;
}
}
return inv;
}
void solve() {
i32 n, k;
cin >> n >> k;
Vec<i32> a(n);
REP(i, n) {
cin >> a[i];
}
if (k == n) {
cout << 0 << '\n';
return;
}
// divide into k segments
++k;
CoordinateCompression<i32> cc(a);
DBG(a);
Vec<Vec<usize>> invs = calc_inv(a);
DBG(invs);
const auto relaxed = [&](i64 lambda) -> i64 {
Vec<i64> dp(n + 1, INF64);
dp[0] = 0;
REP(i, 1, n + 1) {
REP(j, i) {
chmin(dp[i], dp[j] + invs[j][i] + lambda);
}
}
return dp[n] - lambda * k;
};
i64 ans = golden_search(relaxed, (i64)(-n * n), (i64) (n * n), greater{}).second;
cout << ans << '\n';
}
int main() {
int t;
std::cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3388kb
input:
2 6 1 4 5 6 2 2 1 6 2 4 5 6 2 2 1
output:
2 0
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 4ms
memory: 3456kb
input:
349 6 2 2 1 2 1 2 1 48 12 42 47 39 39 27 25 22 44 45 13 5 48 38 4 37 6 24 10 42 38 12 37 31 19 44 6 29 17 7 12 7 26 35 24 15 9 37 3 27 21 33 29 34 20 14 30 31 21 48 12 1 43 17 46 17 39 40 22 25 2 22 12 4 11 38 12 4 11 1 5 39 44 37 10 19 20 42 45 2 45 37 20 48 34 16 41 23 18 13 44 47 21 29 4 23 18 16...
output:
1 18 15 145 0 2 1 0 13 13 23 0 0 0 1 0 0 0 0 0 0 0 0 161 3 0 0 1 0 0 0 0 0 0 1 0 3 0 0 1 0 0 1 0 0 1 4 0 0 0 0 0 0 0 0 2 0 2 0 0 8 280 0 0 34 4 0 1 0 0 3 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 2 0 0 0 0 0 0 0 8 1 8 0 0 0 0 1 11 5 0 0 0 6 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 13 1 0 0 0 ...
result:
ok 349 lines
Test #3:
score: 0
Accepted
time: 127ms
memory: 11008kb
input:
6 1000 333 84886 84887 84732 83775 83776 83777 81234 80288 79661 79243 79244 78520 78521 78522 78523 78524 78525 79041 79042 78509 78120 77073 77432 77433 76111 75362 75363 75364 75365 75366 73979 73980 73981 73982 73983 73984 73472 73473 73075 72948 72949 72727 72728 72383 72384 72272 72273 72274 7...
output:
0 15 683 156 8 242025
result:
ok 6 lines
Test #4:
score: 0
Accepted
time: 6880ms
memory: 284968kb
input:
1 6000 1000 35 111 78 14 3 104 13 88 52 138 47 116 208 21 169 90 149 132 146 223 65 193 176 174 175 233 18 164 102 141 163 159 48 85 184 201 215 237 89 139 179 172 68 73 216 80 143 221 61 60 42 207 219 16 43 225 120 44 1 196 157 202 194 137 156 145 27 40 70 217 170 91 77 92 34 54 29 51 140 198 49 18...
output:
847
result:
ok single line: '847'
Test #5:
score: 0
Accepted
time: 6684ms
memory: 284892kb
input:
1 6000 2990 1500 1499 1498 1497 1496 1495 1494 1493 1492 1491 1490 1489 1488 1487 1486 1485 1484 1483 1482 1481 1480 1479 1478 1477 1476 1475 1474 1473 1472 1471 1470 1469 1468 1467 1466 1465 1464 1463 1462 1461 1460 1459 1458 1457 1456 1455 1454 1453 1452 1451 1450 1449 1448 1447 1446 1445 1444 144...
output:
8
result:
ok single line: '8'
Test #6:
score: 0
Accepted
time: 6756ms
memory: 284860kb
input:
1 6000 2442 180 179 178 177 176 175 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 ...
output:
15
result:
ok single line: '15'
Test #7:
score: 0
Accepted
time: 6958ms
memory: 284932kb
input:
1 6000 1100 587239732 341164140 291813224 988290120 53068601 753624625 858461092 949829430 434273763 366369033 221857040 941264717 466308039 367346550 175500843 354302898 380134504 706309233 531982203 378662525 758240314 649219920 350560644 674837065 874382954 509572170 167839902 336312571 979805021...
output:
3556
result:
ok single line: '3556'
Test #8:
score: 0
Accepted
time: 6912ms
memory: 284960kb
input:
1 6000 592 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 5969 5970 5971 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961...
output:
2
result:
ok single line: '2'
Test #9:
score: 0
Accepted
time: 6764ms
memory: 284912kb
input:
1 6000 5998 6000000 5999000 5998000 5997000 5996000 5995000 5994000 5993000 5992000 5991000 5990000 5989000 5988000 5987000 5986000 5985000 5984000 5983000 5982000 5981000 5980000 5979000 5978000 5977000 5976000 5975000 5974000 5973000 5972000 5971000 5970000 5969000 5968000 5967000 5966000 5965000 ...
output:
1
result:
ok single line: '1'