QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#378756 | #8574. Swirly Sort | ucup-team987# | AC ✓ | 41ms | 6372kb | C++23 | 6.5kb | 2024-04-06 14:31:36 | 2024-04-06 14:31:36 |
Judging History
answer
#if __INCLUDE_LEVEL__ == 0
#include __BASE_FILE__
namespace {
// https://atcoder.jp/contests/arc163/tasks/arc163_f/editorial
i64 f(const std::vector<int>& a) {
int n = len(a);
std::priority_queue<int> q;
i64 ret = 0;
for (const int i : rep(n)) {
q.push(a[i]);
q.push(a[i]);
const int v = q.top();
q.pop();
ret += std::max(v - a[i], 0);
}
return ret;
}
void solve() {
int n, k;
scan(n, k);
std::vector<int> a(n);
scan(a);
if (k == 1) {
print(f(a));
} else if (k == n) {
i64 ans = inf<i64>();
for (int _ = n; _--;) {
chmin(ans, f(a));
ranges::rotate(a, a.begin() + 1);
}
print(ans);
} else if (k % 2 == 0) {
print(0);
} else {
auto b = a;
ranges::sort(b);
if (ranges::adjacent_find(b) != b.end()) {
print(0);
} else {
atcoder::fenwick_tree<int> f(n);
i64 inv = 0;
for (const int i : rep(n)) {
const int t = static_cast<int>(ranges::lower_bound(b, a[i]) - b.begin());
inv += f.sum(t, n);
f.add(t, 1);
}
if (inv % 2 == 0) {
print(0);
} else {
int ans = inf();
for (const int i : rep(1, n)) {
chmin(ans, b[i] - b[i - 1]);
}
print(ans);
}
}
}
}
} // namespace
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::setprecision(DBL_DECIMAL_DIG);
int t;
scan(t);
while (t--) {
solve();
}
}
#else // __INCLUDE_LEVEL__
#include <bits/stdc++.h>
namespace atcoder {
namespace internal {
template <class T>
using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type, std::false_type>::type;
template <class T>
using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type, std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>;
template <class T>
using is_integral =
typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type, std::false_type>::type;
template <class T>
using is_signed_int =
typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type, std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type, std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value, make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>,
std::common_type<T>>::type>::type;
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T>
using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
namespace atcoder {
template <class T>
struct fenwick_tree {
using U = internal::to_unsigned_t<T>;
public:
fenwick_tree() : _n(0) {}
explicit fenwick_tree(int n) : _n(n), data(n) {}
void add(int p, T x) {
assert(0 <= p && p < _n);
p++;
while (p <= _n) {
data[p - 1] += U(x);
p += p & -p;
}
}
T sum(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
return sum(r) - sum(l);
}
private:
int _n;
std::vector<U> data;
U sum(int r) {
U s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
};
} // namespace atcoder
template <class T, class U = T>
bool chmin(T& x, U&& y) {
return y < x && (x = std::forward<U>(y), true);
}
template <class T, class U = T>
bool chmax(T& x, U&& y) {
return x < y && (x = std::forward<U>(y), true);
}
template <std::signed_integral T = int>
T inf() {
T ret;
std::memset(&ret, 0x3f, sizeof(ret));
return ret;
}
template <std::floating_point T>
T inf() {
return std::numeric_limits<T>::infinity();
}
template <class T>
concept Range = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;
template <class T>
concept Tuple = std::__is_tuple_like<T>::value && !Range<T>;
namespace std {
istream& operator>>(istream& is, Range auto&& r) {
for (auto&& e : r) {
is >> e;
}
return is;
}
istream& operator>>(istream& is, Tuple auto&& t) {
return apply([&](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}
ostream& operator<<(ostream& os, Range auto&& r) {
for (string_view sep = ""; auto&& e : r) {
os << exchange(sep, " ") << e;
}
return os;
}
ostream& operator<<(ostream& os, Tuple auto&& t) {
const auto f = [&](auto&... xs) -> ostream& {
[[maybe_unused]] string_view sep = "";
((os << exchange(sep, " ") << xs), ...);
return os;
};
return apply(f, t);
}
} // namespace std
void scan(auto&&... xs) { std::cin >> std::tie(xs...); }
void print(auto&&... xs) { std::cout << std::tie(xs...) << '\n'; }
template <class F>
class fix {
public:
explicit fix(F f) : f_(std::move(f)) {}
decltype(auto) operator()(auto&&... xs) const {
return f_(std::ref(*this), std::forward<decltype(xs)>(xs)...);
}
private:
F f_;
};
inline auto rep(int l, int r) { return std::views::iota(std::min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }
using namespace std::literals;
namespace ranges = std::ranges;
namespace views = std::views;
using i64 = std::int64_t;
#define len(...) static_cast<int>(ranges::size(__VA_ARGS__))
#endif // __INCLUDE_LEVEL__
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3828kb
input:
4 4 1 6 4 3 7 4 2 6 4 3 7 4 3 6 4 3 7 4 4 6 4 3 7
output:
3 0 1 2
result:
ok 4 number(s): "3 0 1 2"
Test #2:
score: 0
Accepted
time: 8ms
memory: 3524kb
input:
10000 4 3 524728 254456 277709 19127 15 11 360089 525234 862619 897281 336644 910706 75922 708901 754517 734744 94169 326125 746826 846063 159956 4 2 140105 792522 40264 514789 12 2 270333 888927 500833 9065 936673 982631 332435 751429 607700 840339 804685 416612 8 7 119416 689632 517277 673646 8262...
output:
23253 7691 0 0 15986 278544 0 0 0 0 0 2022 0 0 0 9260 0 0 51255 0 0 277173 480146 0 658 4525 0 0 0 0 0 266148 0 767231 5853 0 0 121885 0 788638 0 0 0 779611 0 5881 0 0 0 0 517074 0 0 0 210836 454586 662851 0 781542 0 0 864957 175421 0 0 0 0 0 0 0 541010 0 0 15407 0 0 3413333 0 0 0 0 19677 30305 3095...
result:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 8ms
memory: 3804kb
input:
10000 1 1 2246872 10 1 6956989 9799253 5103131 200665 8599026 7743840 6622177 9299599 4771199 2388897 1 1 4115997 2 1 2246219 2946703 1 1 8870887 5 4 4465846 6917492 4431029 8967539 33631 11 11 5721411 592798 6930331 6862401 4082972 2094477 1505423 2091687 9125024 8518457 361077 4 2 8818946 4073615 ...
output:
0 23312638 0 0 0 0 17919297 0 543116 0 0 783241 0 44991 0 0 0 4721212 0 11367749 0 0 421992 0 4267974 0 0 0 0 0 0 0 0 1172214 0 0 0 0 0 9209019 0 0 5365348 922347 463528 10588447 0 0 0 2144103 17190623 19634388 142708 6877382 0 0 0 0 0 0 0 0 0 1477236 0 0 0 0 0 0 820573 0 0 0 3767488 8960620 0 10561...
result:
ok 10000 numbers
Test #4:
score: 0
Accepted
time: 8ms
memory: 3572kb
input:
10000 2 1 45149197 33261068 5 1 55118470 16401191 17389202 89510782 34998353 5 5 63464501 21878886 62995140 27832883 54891420 7 2 85638582 825401 81784814 21532809 30150850 21800436 94882138 2 2 83299527 63718695 3 1 89482904 64518505 91301116 1 1 82256621 1 1 30148988 3 1 68107859 50635233 8682010 ...
output:
11888129 93229708 35162257 0 0 24964399 0 0 59425849 0 22479259 5248308 0 0 0 41327792 0 207654 0 0 0 0 0 0 68210620 0 0 194925 0 0 73467281 33859825 122226 74005621 26201400 0 119179402 0 0 0 0 0 0 0 816171 0 0 0 25910307 3451662 0 4390900 0 0 0 22895459 0 0 0 102364933 0 346465 0 0 0 0 0 58190487 ...
result:
ok 10000 numbers
Test #5:
score: 0
Accepted
time: 8ms
memory: 3632kb
input:
10000 3 2 171561989 326460559 568826834 4 1 606735910 34072129 203284467 873417326 1 1 436249551 3 2 866901680 525830568 142746353 14 11 742357529 676987595 673771185 430647518 327098063 734643932 300528112 859509055 593973771 842011838 467635682 368399037 810057370 576054534 3 2 822197945 247906143...
output:
0 572663781 0 0 3216410 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 696031608 0 559141551 975330575 0 0 0 0 8269118 0 1645243801 0 0 0 0 0 480200189 444013173 1223177234 0 17211999 0 0 0 364069727 178337070 5296068017 0 0 0 0 709411979 0 2420843006 192213554 238004067 0 0 0 0 0 0 0 0 2626223 249160097 1183427809 ...
result:
ok 10000 numbers
Test #6:
score: 0
Accepted
time: 29ms
memory: 3652kb
input:
10000 11 1 719994 371444 177473 165906 258003 388506 556396 344249 756298 954668 668450 37 1 154783 680471 27958 18537 684073 711211 924310 535353 766034 510375 800832 64509 814950 546211 134844 3096 877063 837800 722279 626142 168108 336054 747182 590551 161949 182719 495638 869503 252408 315050 73...
output:
1246424 7880204 1036121 8010082 4745853 18497805 5589959 3845644 68400 265726 6056838 7202823 250222 191468 0 15682644 2627976 25524438 8162775 8408018 23822003 11083830 6961866 3133712 1753363 7922672 9894486 13936678 134033 11202387 3724366 10564706 3652183 3657586 17282350 540513 0 11530473 0 397...
result:
ok 10000 numbers
Test #7:
score: 0
Accepted
time: 32ms
memory: 3660kb
input:
10000 47 1 523059617 328055632 781822408 940589336 455730858 863296197 437030049 658265760 184918189 919959759 734943860 873150203 125605001 626490418 539172080 208944909 697529825 878806549 470244556 457799266 672503197 560798341 691187776 875798951 961299047 680353009 720548746 61867822 924965167 ...
output:
11720848879 20340520024 12486685858 1461710069 6313943138 31665376872 5928212025 1808940644 3787048058 2036744900 1426130451 2146871925 11531760949 4856941583 1425720379 2321337929 1320345528 1327213403 1405424545 8384107070 18369882866 11356718569 0 5267428466 636832484 16157808062 22122796995 6468...
result:
ok 10000 numbers
Test #8:
score: 0
Accepted
time: 41ms
memory: 6372kb
input:
1 300000 1 651102162 912784062 280750844 923640913 5054227 251711260 332945151 660425685 157943264 621946056 560328273 748436147 275105264 126861296 617420574 581173721 857525507 142861415 241368020 281859612 367236952 16104994 157324711 178667188 39607861 614078909 490652942 864292286 305586786 135...
output:
74906657059416
result:
ok 1 number(s): "74906657059416"
Test #9:
score: 0
Accepted
time: 13ms
memory: 4004kb
input:
1 100000 3 602375124 875161080 566363405 109583770 377899457 133319437 709759298 996340891 319888403 539626667 428135630 351615811 924398920 83068878 810553330 758478111 982972289 862464949 529972433 57016896 968624390 364490851 764434932 444421051 683535040 10290976 549700807 263440426 278436542 86...
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: 0
Accepted
time: 14ms
memory: 3660kb
input:
2000 31 3 698810268 462004468 967891238 433071494 190111534 782757983 927040377 733347091 50578740 573583315 417745137 643098900 938483951 301712753 279989662 232161232 536757345 660117956 31789717 511162930 3842769 548739126 343996527 258630727 230356299 750829976 945790410 493372865 997436190 6316...
output:
0 4951 0 0 347770 15452560 5060292 6992 0 2720779 0 0 0 882725 53291879 0 939124 33861516 4405688 0 1781066 186310 28661615 7484769 0 0 28536937 0 1706581 580060 0 13419408 0 1909152 931874 1062952 230576 0 0 0 0 0 0 0 4263895 69366 1426433 0 0 0 0 642345 142664 0 0 2956752 513249 0 0 2993400 0 3895...
result:
ok 2000 numbers
Test #11:
score: 0
Accepted
time: 7ms
memory: 3616kb
input:
1 100000 2 477476356 94013914 625859012 730906024 611051981 241413132 629052354 340768318 842017659 956572518 612192775 286707518 859673844 186614005 442273229 796755030 179975139 893706537 184792656 362272473 138300473 94847574 221835090 267842108 416763558 818622772 70940568 971541146 497308964 29...
output:
0
result:
ok 1 number(s): "0"
Test #12:
score: 0
Accepted
time: 9ms
memory: 3676kb
input:
2000 26 2 924003662 871229099 358864902 330638562 825068362 163072451 54148927 515858219 543157954 724050502 993751520 272486178 839501706 220518782 220448393 36073704 527761327 75640326 325605198 974218035 740338158 430636621 569817145 280599500 428848698 961283863 58 2 822664646 398219282 18343378...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 2000 numbers
Test #13:
score: 0
Accepted
time: 8ms
memory: 3628kb
input:
18555 1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 2 1 1 1 2 2 1 1 2 1 1 2 2 2 1 2 2 1 1 3 2 2 1 3 2 1 1 4 2 2 1 4 2 1 1 5 2 2 1 5 2 1 2 1 2 2 2 1 2 1 2 2 2 2 2 2 2 1 2 3 2 2 2 3 2 1 2 4 2 2 2 4 2 1 2 5 2 2 2 5 2 1 3 1 2 2 3 1 2 1 3 2 2 2 3 2 2 1 3 3 2 2 3 3 2 1 3 4 2 2 3 4 2 1 3 5 2 2 3 5 2 1 4 1 2 2 4 1 2 1 4 2 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 3 0 2 0 1 0 0 0 0 0 4 0 3 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 1 0 1 0 0 0 0 0 0 0 0 0 3 0 0 2 0 1 1 0 1 0 0 0 0 0 0 4 0 0 3 0 1 2 0 2 1 0 1 0 0 0 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 ...
result:
ok 18555 numbers
Test #14:
score: 0
Accepted
time: 24ms
memory: 4452kb
input:
1 100000 3 103670965 730988733 612690321 859510898 495420570 617006158 930838259 781270919 815385999 657404732 838079716 160695659 234318868 13088314 849798902 74279803 560806478 676215272 38150915 858044286 667276814 994119002 698379276 449347578 713488988 862195451 930978455 362968914 387608599 23...
output:
1
result:
ok 1 number(s): "1"
Test #15:
score: 0
Accepted
time: 20ms
memory: 3636kb
input:
1 547 547 887954172 384424822 567917975 563531012 392348181 376515730 505356706 322940528 557433012 539277779 668097503 50062339 993650038 144040651 646365158 970239629 830237517 873351107 148978812 731914101 440991373 825216126 275919951 769751372 819414582 351059 208172908 119263053 998452139 5550...
output:
132549084799
result:
ok 1 number(s): "132549084799"
Test #16:
score: 0
Accepted
time: 20ms
memory: 3520kb
input:
1 546 546 4305572 374272421 88147916 554446056 930778731 238301666 878297114 646261853 194755102 312781870 225149932 872612884 331270197 903800554 944512335 156116735 840475042 491427606 984195 990974885 73381054 192504546 4969187 310585949 951311970 425024295 590896048 818902913 464200784 158688832...
output:
127477085741
result:
ok 1 number(s): "127477085741"
Test #17:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
100 157 23 846216104 325976483 76823620 279509806 242790881 9676858 792345559 322056553 632488502 444723554 70422962 27172325 26697435 40309279 992120464 751627327 499482549 277429832 736386595 471708200 940778720 417787658 306890639 248789992 1849130 203226467 672298989 938207958 331602581 31374660...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18748 0 0 0 44618 0 0 1734 0 0 0 37551 0 0 0 0 9738 79158 497404 0 0 0 0 0 0 0 0 32019 0 0 123787 10539 178749 0 0 0 86176 0 115850 34488 0 0 0 0 153492 0 0 0 0 0 0 0 1010455 0 363342 60486 0 233210 0 0 0 0 1145801 0 0 93911 0 0 0 0 0 0 0 0 17885 0 0 0 0 0 0 0 0 320335
result:
ok 100 numbers
Test #18:
score: 0
Accepted
time: 2ms
memory: 3724kb
input:
10 1928 24 971407775 628894325 731963982 822804784 450968417 430302156 982631932 161735902 880895728 923078537 707723857 189330739 910286918 802329211 404539679 303238506 317063340 492686568 773361868 125660016 650287940 839296263 462224593 492601449 384836991 191890310 576823355 782177068 404011431...
output:
0 0 1065 0 0 0 75358 0 39 5
result:
ok 10 numbers
Extra Test:
score: 0
Extra Test Passed