QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#340085 | #1961. Postman | ucup-team1209# | WA | 1ms | 7636kb | C++20 | 3.7kb | 2024-02-28 14:31:31 | 2024-02-28 14:31:31 |
Judging History
answer
#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
using ll = long long ;
cs int N = 3e5 + 5;
int n, W, E, op;
int a[N];
ll ans = 1e18;
void cmn(ll & x, ll y) {
if(x > y) x = y;
}
void work(int zero, int las, int e, int w) {
// [1, zero) [zero , n ]
ll sum = 0;
if(zero > 1) {
sum += -2 * a[1];
w -= 1;
}
if(las != n) {
sum += 2 * (a[n] - a[las]);
w -= 1;
}
sum += a[las];
if(w < 0) return;
e -= 1;
if(las > zero) e -= 1;
if(e < 0) return;
if(las == zero) {
cmn(ans, sum);
return;
}
multiset <ll> s;
for(int i = zero; i + 1 < las; i++) {
s.insert(a[i + 1] - a[i]);
}
e -= s.size();
while (e < 0) sum += *(s.begin()) * 2, ++ e, s.erase(s.begin());
cmn(ans, sum);
}
void work1(int zero, int las, int E, int W) {
if(W == 1) cmn(ans, a[n] + a[n] - a[1] + a[las] - a[1]);
}
int root;
namespace sgt {
#define mid ((l + r) >> 1)
cs int N = :: N * 32;
ll sum[N];
int lc[N], rc[N], sz[N], nd;
void clr() {
memset(lc, 0, (nd + 1) << 2);
memset(rc, 0, (nd + 1) << 2);
memset(sum, 0, (nd + 1) << 3);
memset(sz, 0, (nd + 1) << 2);
nd = 0;
}
void ins(int &x, int l, int r, int c) {
if(x == 0) x = ++ nd;
sz[x] += 1, sum[x] += c;
if(l == r) return ;
if(c <= mid) ins(lc[x], l, mid, c);
else ins(rc[x], mid + 1, r, c);
}
ll qry(int x, int l, int r, int k) {
if(l == r) return 1ll * k * l;
if(k <= sz[lc[x]]) return qry(lc[x], l, mid, k);
return qry(rc[x], mid + 1, r, k - sz[lc[x]]) + sum[lc[x]];
}
#undef mid
}
void rev(int & las) {
for(int i = 1; i <= n; i++) a[i] *= -1;
swap(E, W);
las = a[n];
}
void work2(int zero, int las, int e, int w) {
// [1, zero) [zero , n ]
ll sum = 0;
if(zero > 1) {
sum += -2 * a[1];
w -= 1;
}
if(las != n) {
sum += 2 * (a[n] - a[las]);
w -= 1;
}
sum += a[las];
if(w < 0) return;
e -= 1;
if(las > zero) e -= 1;
if(e < 0) return;
if(las == zero) {
cmn(ans, sum);
return;
}
e -= sgt :: sz[root];
sum += 2ll * sgt :: qry(root, 0, 1e9, -e);
cmn(ans, sum);
}
int main() {
#ifdef zqj
freopen("1.in","r",stdin);
#endif
ios :: sync_with_stdio(0), cin.tie(0);
cin >> n >> W >> op;
E = n - W;
for(int i = 1; i <= n; i++)
cin >> a[i];
int las = a[n];
if(a[n] < 0) {
rev(las);
}
sort(a + 1, a + n + 1);
a[n + 1] = 1e9 + 7;
las = lower_bound(a + 1, a + n + 1, las) - a;
int zero = lower_bound(a + 1, a + n + 2, 0) - a;
// [1, zero) [zero , n ]
if(op == 1) {
for(int las = zero; las <= n; las ++) {
// a[las - 2] -- a[las - 1]
if(las - 2 >= zero) {
sgt :: ins(root, 0, 1e9, a[las - 1] - a[las - 2]);
}
work2(zero, las, E, W);
}
rev(las);
sort(a + 1, a + n + 1);
a[n + 1] = 1e9 + 7;
las = lower_bound(a + 1, a + n + 1, las) - a;
zero = lower_bound(a + 1, a + n + 2, 0) - a;
sgt :: clr();
root = 0;
for(int las = zero; las <= n; las ++) {
// a[las - 2] -- a[las - 1]
if(las - 2 >= zero) {
sgt :: ins(root, 0, 1e9, a[las - 1] - a[las - 2]);
}
work2(zero, las, E, W);
}
cout << ans << '\n';
return 0;
}
else {
work1(zero, las, E, W);
work(zero, las, E, W);
cout << ans << '\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 7636kb
input:
100 0 1 751 558 131 292 317 886 785 847 224 668 649 358 725 250 953 65 176 733 461 84 114 977 628 673 798 863 373 827 51 630 518 347 527 876 366 241 452 670 813 314 846 342 357 460 985 581 841 843 282 907 327 656 411 944 421 99 441 388 568 763 167 351 793 916 517 109 999 390 272 639 513 534 304 253 ...
output:
999
result:
ok single line: '999'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5740kb
input:
100 50 1 654 349 533 94 619 754 498 939 469 518 3 661 431 214 195 198 989 548 561 312 262 629 807 981 624 639 178 765 706 782 888 611 553 363 336 91 176 485 735 791 596 455 390 760 995 202 417 82 798 883 209 155 725 908 789 56 49 899 231 423 815 228 314 400 748 946 736 840 385 446 938 284 10 917 593...
output:
1397
result:
ok single line: '1397'
Test #3:
score: 0
Accepted
time: 1ms
memory: 5948kb
input:
100 99 1 266 337 80 671 358 778 217 288 219 48 863 19 137 987 602 415 155 398 753 956 210 317 94 303 997 192 123 442 666 959 685 71 34 588 615 214 327 881 582 51 646 630 291 354 749 108 984 308 813 182 384 699 114 692 611 296 590 432 103 240 341 465 969 370 771 914 250 656 742 708 174 323 939 493 17...
output:
1986
result:
ok single line: '1986'
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 5772kb
input:
100 100 1 713 172 634 763 181 502 78 995 598 751 792 961 585 335 952 343 375 627 651 515 856 121 743 931 18 492 726 517 136 143 314 993 80 29 283 427 248 274 114 200 535 94 174 782 818 90 806 88 104 808 910 892 791 462 304 676 67 973 901 891 705 567 475 444 949 846 299 735 233 914 803 27 810 210 396...
output:
1000000000000000000
result:
wrong answer 1st lines differ - expected: '-1', found: '1000000000000000000'