QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#139512 | #5507. Investors | ammardab3an# | TL | 6906ms | 5652kb | C++17 | 4.0kb | 2023-08-13 19:29:24 | 2023-08-13 19:29:27 |
Judging History
answer
// By AmmarDab3an
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define ll int64_t
// typedef unsigned int uint;
// typedef long long int ll;
// typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> iii;
typedef pair<ll, pll> lll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define freopenI freopen("input.txt", "r", stdin);
#define freopenO freopen("output.txt", "w", stdout);
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int x, int y) {
return uniform_int_distribution<int>(x, y)(rng);
}
int mul(int a, int b){
int ret = (1ll * (a%MOD) * (b%MOD)) % MOD;
return (ret+MOD)%MOD;
}
int add(int a, int b){
int ret = (1ll * (a%MOD) + (b%MOD)) % MOD;
return (ret+MOD)%MOD;
}
int pow_exp(int n, int p){
if(!p) return 1;
if(p&1) return mul(n, pow_exp(n, p-1));
int tmp = pow_exp(n, p/2);
return mul(tmp, tmp);
}
int inv(int x){
return pow_exp(x, MOD-2);
}
const int MAX = 2e5 + 10;
const int NMAX = 2e5 + 10;
const int MMAX = 2e5 + 10;
const int LOG_MAX = ceil(log2(double(NMAX)));
const int BLOCK = ceil(sqrt(double(NMAX)));
int fac[NMAX], ifac[NMAX];
void init(){
fac[0] = 1;
for(int i = 1; i < NMAX; i++){
fac[i] = mul(fac[i-1], i);
}
ifac[NMAX-1] = inv(fac[NMAX-1]);
for(int i = NMAX-2; i >= 0; i--){
ifac[i] = mul(ifac[i+1], i+1);
}
}
int choose(int n, int c){
assert(n >= c);
return mul(fac[n], mul(ifac[c], ifac[n-c]));
}
struct FenwickTree {
vector<int> bit; // binary indexed tree
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
int32_t main(){
fastIO;
#ifdef LOCAL
freopenI;
freopenO;
#endif
// freopen("name.in", "r", stdin);
// init();
int t; cin >> t; while(t--){
int n, k;
cin >> n >> k;
k++;
vi vec(n);
for(auto &i : vec) cin >> i;
vi tmp = vec;
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
for(auto &e : vec){
e = lower_bound(tmp.begin(), tmp.end(), e) - tmp.begin();
}
auto calc = [&](int x_factor)->pii{
vpii dp(n);
for(int i = 0; i < n; i++){
FenwickTree bit(n);
pii cans = {INFLL, 0};
int cnt = 0;
for(int j = i; j >= 0; j--){
cnt += bit.sum(0, vec[j]-1);
bit.add(vec[j], 1);
pii nxt = j ? dp[j-1] : (pii){0, 0};
nxt.first += cnt + x_factor;
nxt.second++;
cans = min(cans, nxt);
}
dp[i] = cans;
}
// cout << x_factor << ' ' << dp.back().second << endl;
return dp[n-1];
};
int l = 0;
int r = INF;
int bs_ans = -1;
while(l <= r){
int mid = (l+r)/2;
pii cans = calc(mid);
if(cans.second <= k){
bs_ans = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
assert(bs_ans != -1);
pii ans = calc(bs_ans);
// cout << bs_ans << ' ' << ans.first << ' ' << ans.second << endl;
cout << ans.first - bs_ans*k << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3600kb
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: 20ms
memory: 5652kb
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: 934ms
memory: 3588kb
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: 6906ms
memory: 3708kb
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: 6612ms
memory: 3720kb
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: 6785ms
memory: 3700kb
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: -100
Time Limit Exceeded
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...