QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#439517 | #6158. 魔法商店 | hos_lyric | 100 ✓ | 60ms | 6800kb | C++14 | 6.3kb | 2024-06-12 07:23:11 | 2024-06-12 07:23:11 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
template <class Flow> struct MaxFlow {
// Watch out when using types other than int and long long.
static constexpr Flow FLOW_EPS = 1e-10L;
static constexpr Flow FLOW_INF = std::numeric_limits<Flow>::max();
int n, m;
vector<int> ptr, nxt, zu;
vector<Flow> capa;
explicit MaxFlow(int n_) : n(n_), m(0), ptr(n_, -1) {}
void ae(int u, int v, Flow w0, Flow w1 = 0) {
assert(0 <= u); assert(u < n);
assert(0 <= v); assert(v < n);
assert(0 <= w0);
assert(0 <= w1);
nxt.push_back(ptr[u]); zu.push_back(v); capa.push_back(w0); ptr[u] = m++;
nxt.push_back(ptr[v]); zu.push_back(u); capa.push_back(w1); ptr[v] = m++;
}
vector<int> see, lev, que;
Flow augment(int u, int t, Flow limFlow) {
if (u == t) return limFlow;
for (int &i = see[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
const int v = zu[i];
if (lev[u] < lev[v]) {
const Flow f = augment(v, t, min(limFlow, capa[i]));
if (f > FLOW_EPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
}
}
return 0;
}
bool bfs(int s, int t) {
for (int u = 0; u < n; ++u) { see[u] = ptr[u]; lev[u] = -1; }
auto head = que.begin(), tail = que.begin();
for (lev[*tail++ = s] = 0; head != tail; ) {
const int u = *head++;
for (int i = ptr[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
const int v = zu[i];
if (!~lev[v]) {
lev[*tail++ = v] = lev[u] + 1;
if (v == t) return true;
}
}
}
return false;
}
Flow run(int s, int t, Flow limFlow = FLOW_INF) {
see.resize(n); lev.resize(n); que.resize(n);
Flow flow = 0;
for (; flow + FLOW_EPS < limFlow && bfs(s, t); ) {
for (Flow f; (f = augment(s, t, limFlow - flow)) > FLOW_EPS; flow += f) {}
}
return flow;
}
};
////////////////////////////////////////////////////////////////////////////////
/*
problem
(C[A[1]], ..., C[A[M]]): basis of (C[1], ..., C[N])
(C[B[1]], ..., C[B[M]]): basis of (C[1], ..., C[N])
minimize \sum[u] (w[u] - V[u])^2
subject to:
- A: min cost basis
- B: max cost basis
*/
using u64 = unsigned long long;
constexpr int E = 64;
Int sq(Int w) {
return w * w;
}
int N, M;
vector<u64> C;
vector<Int> V;
vector<int> A[2];
int main() {
for (; ~scanf("%d%d", &N, &M); ) {
C.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%llu", &C[u]);
}
V.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%lld", &V[u]);
}
for (int h = 0; h < 2; ++h) {
A[h].resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d", &A[h][i]);
--A[h][i];
}
}
// w[u] < w[v]
vector<pair<int, int>> edges;
for (int h = 0; h < 2; ++h) {
u64 basis[E] = {}, masks[E] = {};
for (int i = 0; i < M; ++i) {
u64 c = C[A[h][i]];
u64 p = 1ULL << i;
for (int e = E; --e >= 0; ) if (c >> e & 1) {
c ^= basis[e];
p ^= masks[e];
}
assert(c);
const int e = 63 - __builtin_clzll(c);
basis[e] = c;
masks[e] = p;
}
for (int u = 0; u < N; ++u) {
u64 c = C[u];
u64 p = 0;
for (int e = E; --e >= 0; ) if (c >> e & 1) {
c ^= basis[e];
p ^= masks[e];
}
assert(!c);
for (int i = 0; i < M; ++i) if (p >> i & 1) {
if (A[h][i] != u) {
if (h == 0) {
edges.emplace_back(A[h][i], u);
} else {
edges.emplace_back(u, A[h][i]);
}
}
}
}
}
// cerr<<"edges = "<<edges<<endl;
vector<Int> los(N, *min_element(V.begin(), V.end()));
vector<Int> his(N, *max_element(V.begin(), V.end()));
for (; ; ) {
bool any = false;
vector<Int> wss[2];
for (int z = 0; z < 2; ++z) {
wss[z].resize(N);
}
for (int u = 0; u < N; ++u) {
if (los[u] < his[u]) {
any = true;
wss[0][u] = (los[u] + his[u]) / 2;
wss[1][u] = wss[0][u] + 1;
} else {
wss[0][u] = wss[1][u] = los[u];
}
}
if (!any) break;
// src: wss[0], snk: wss[1]
MaxFlow<Int> mf(N + 2);
const int src = N, snk = N + 1;
for (int u = 0; u < N; ++u) {
const Int d = sq(wss[1][u] - V[u]) - sq(wss[0][u] - V[u]);
if (d >= 0) {
mf.ae(src, u, d);
} else {
mf.ae(u, snk, -d);
}
}
for (const auto &e : edges) {
if (wss[1][e.first] > wss[0][e.second]) {
mf.ae(e.second, e.first, mf.FLOW_INF);
}
}
mf.run(src, snk);
for (int u = 0; u < N; ++u) {
if (~mf.lev[u]) {
his[u] = wss[0][u];
} else {
los[u] = wss[1][u];
}
}
// cerr<<"los = "<<los<<", his = "<<his<<endl;
}
Int ans = 0;
for (int u = 0; u < N; ++u) {
ans += sq(los[u] - V[u]);
}
printf("%lld\n", ans);
}
return 0;
}
詳細信息
Test #1:
score: 5
Accepted
time: 1ms
memory: 3788kb
input:
10 4 12 14 13 11 11 6 12 14 4 6 2 2 3 1 2 4 3 1 2 2 4 7 3 9 8 10 5 1
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 5
Accepted
time: 0ms
memory: 3788kb
input:
10 4 537919488 537919616 537919616 546308096 536871040 536871040 546308096 546308224 536870912 546308224 5 3 5 2 2 3 1 5 5 3 1 6 8 9 2 5 1 10
output:
17
result:
ok 1 number(s): "17"
Test #3:
score: 5
Accepted
time: 0ms
memory: 4080kb
input:
10 4 10 15 7 15 15 7 12 12 15 12 2 4 5 3 4 2 2 1 2 2 10 2 1 6 7 4 6 1
output:
9
result:
ok 1 number(s): "9"
Test #4:
score: 5
Accepted
time: 0ms
memory: 3784kb
input:
50 2 16384 16392 16384 16392 16392 16392 8 16384 16384 16384 16392 16384 16392 16392 16392 16384 16392 16384 8 16392 16392 16392 16384 8 16392 16392 16392 8 16384 16392 8 16384 16392 8 8 16392 16392 16384 16392 16392 16384 16392 16384 16384 8 16392 8 16384 16392 16384 8 6 3 6 8 10 3 9 6 9 3 9 7 1 7 ...
output:
124
result:
ok 1 number(s): "124"
Test #5:
score: 5
Accepted
time: 1ms
memory: 4092kb
input:
50 2 3 3 2 1 2 2 3 2 3 3 3 3 3 3 2 3 3 3 3 3 3 3 1 2 3 1 2 2 2 3 3 2 3 1 2 1 1 1 1 3 1 2 2 2 3 2 1 3 3 2 8 6 5 3 5 6 9 8 8 4 8 1 8 2 3 8 3 9 7 7 7 4 5 5 4 10 2 7 8 7 10 4 9 3 4 1 6 6 9 7 9 8 8 9 1 6 6 9 6 10 50 30 15 25
output:
132
result:
ok 1 number(s): "132"
Test #6:
score: 5
Accepted
time: 1ms
memory: 4072kb
input:
50 2 512 4194304 512 4194816 4194816 4194304 512 4194816 4194304 4194816 4194816 512 512 4194304 512 4194816 4194304 4194304 4194816 512 4194304 4194816 4194816 4194816 512 4194816 4194816 512 4194816 4194304 512 4194816 4194816 4194816 512 4194816 4194816 4194816 4194816 4194816 512 4194304 4194816...
output:
166
result:
ok 1 number(s): "166"
Test #7:
score: 5
Accepted
time: 2ms
memory: 4060kb
input:
500 30 741719510 646445985 288528865 130324436 1022674617 794527463 415832068 696488643 894375667 529476111 616320251 1029690979 734912409 248189699 172160308 945539459 847444438 515073105 500736852 605995961 546746534 277230057 733100842 50105520 173158903 749974153 936777435 482380146 488416539 83...
output:
234
result:
ok 1 number(s): "234"
Test #8:
score: 5
Accepted
time: 2ms
memory: 4008kb
input:
500 30 1990070654 2016726546 595122380 1833221392 1148980864 648515290 1737302388 978988136 443014858 28662028 663417794 907384830 1366145510 965217404 1979597170 747538274 1223764674 1286163142 224717864 743467006 1688552000 1695212326 768903554 1981800968 1632313764 1088795832 2110574780 275644596...
output:
136
result:
ok 1 number(s): "136"
Test #9:
score: 5
Accepted
time: 0ms
memory: 4084kb
input:
500 30 396525161 730488792 1070296808 1038120642 836361159 525804317 198381177 326381967 271274865 454980465 1004597166 245568040 743253949 58671044 323749003 174979889 859234637 1022712333 92827877 183853577 174565035 532752030 672886470 808732531 417882843 764705738 431992315 624223351 389029448 1...
output:
224
result:
ok 1 number(s): "224"
Test #10:
score: 5
Accepted
time: 2ms
memory: 4072kb
input:
500 30 1050239093 1275100070 1280234241 1109117441 1525229993 2100447407 1392630340 867686664 2078466398 728947032 1340332709 1745276952 982711053 17463689 2111756409 1782059773 1979034525 898746648 667281610 970062237 678543545 1303431361 6910874 576672089 653172948 1214440356 1095832139 1841637874...
output:
230
result:
ok 1 number(s): "230"
Test #11:
score: 5
Accepted
time: 56ms
memory: 6800kb
input:
1000 64 7489760721683908232 895672193265243649 7716995978090800942 15052994623985143895 1313150712678959590 2696018479554129451 1053996564257040209 8826472950109582211 4799022966732178228 568410811400389832 7799116518474076411 6783315873253952302 7900684424871280574 3916594448158730581 1690259201563...
output:
79188355928513
result:
ok 1 number(s): "79188355928513"
Test #12:
score: 5
Accepted
time: 60ms
memory: 6660kb
input:
1000 64 7425562747897104535 4794584406608351330 994307622262521110 15578163978223625508 12758518899629204637 5465272055457059315 5243230727542430196 5661954119488419190 8977133755619281542 11599472068397064198 8580559853872551337 14158951294909789456 13704294257565461582 5441008667885288829 13946622...
output:
83403160186959
result:
ok 1 number(s): "83403160186959"
Test #13:
score: 5
Accepted
time: 14ms
memory: 5796kb
input:
1000 64 2490187386084859617 3469644381879355438 7505398019427703742 14260914126768471876 17768955427049023583 4532305146223186261 11324503569519981965 8012870630977207957 12917253838764476864 508030959024267367 1500765545539106724 5566933568559255272 5859241087780497121 8139642414805716270 105070634...
output:
82898969014066
result:
ok 1 number(s): "82898969014066"
Test #14:
score: 5
Accepted
time: 19ms
memory: 5800kb
input:
1000 64 4935098346720628435 10621970201514995434 12434196617073829208 16436463100645031818 17582562269614054694 12445543130976250588 14337194754832321847 3472799759245626988 13676831487054289097 32589674297842260 12600024203983010652 80354738051700978 14919917380444800224 14808113154185167508 106143...
output:
77538147157083
result:
ok 1 number(s): "77538147157083"
Test #15:
score: 5
Accepted
time: 17ms
memory: 5788kb
input:
1000 64 5047087070090745337 17605595705782503964 3367442785899272766 12000415115769275520 2407734747349751005 16913462195031379102 4578063823366955987 17434142494882750599 797194588819769525 8596568434758432762 932640988558370387 13774777187973870016 9021877276516743186 3409693243821866629 115022799...
output:
82789840580162
result:
ok 1 number(s): "82789840580162"
Test #16:
score: 5
Accepted
time: 11ms
memory: 5860kb
input:
1000 64 17526690810895580076 9104644210459996874 5868977111596174010 8884317979432767036 5816424085659157944 14202608383857555204 17566255438908885816 15213767301694008176 18203533552553030885 6900792885456197325 15995379616891931289 11094461672132263509 17229241847140678376 14037250092223681488 133...
output:
44907762693461
result:
ok 1 number(s): "44907762693461"
Test #17:
score: 5
Accepted
time: 19ms
memory: 5852kb
input:
1000 64 10138461533144689035 619959967155879139 3367359586185354757 7132739989416334330 7755411503933123413 6831667502339005653 14391759158377676701 11240785021673001698 2099753286524408804 8541718509038160569 483916210269132345 5775305984795620274 428219938610044292 15410100208301107560 16943083494...
output:
77528983664344
result:
ok 1 number(s): "77528983664344"
Test #18:
score: 5
Accepted
time: 50ms
memory: 6560kb
input:
1000 64 8726643594807043616 6279063649281433730 2803209117307038106 13367773576704916807 18171400216467582343 15366803877782612997 2800327133909500518 2285697303069511510 9992202909669287142 12688920368537549104 14543013029216016425 3164074802949401677 1748609072597072735 14738911972414013431 314593...
output:
76074870687766
result:
ok 1 number(s): "76074870687766"
Test #19:
score: 5
Accepted
time: 16ms
memory: 6076kb
input:
1000 64 1016334752853672715 828845343652026418 17913760751696355430 5294618425060467033 12376623189033612344 16949011664184882094 1393155786459707030 16646778682675800219 11426408963129658731 14318572197685982785 9802083932370677603 8968613107845232978 6705885342340298381 4514240406550195130 1532882...
output:
82499263313573
result:
ok 1 number(s): "82499263313573"
Test #20:
score: 5
Accepted
time: 14ms
memory: 5852kb
input:
1000 64 8856964183297178024 1181635195005718877 15113440712770365712 9387288686588926157 14344351153892111519 447821168840344047 12850018389468754479 12235243495403301114 5134378439063708013 13550599952171964659 2156442829925769587 9051398569742430396 17586048335758879698 14763246432106696307 124122...
output:
73902374926192
result:
ok 1 number(s): "73902374926192"
Extra Test:
score: 0
Extra Test Passed