QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#307494 | #8005. Crossing the Border | ucup_team_qiuly# | WA | 37ms | 9112kb | C++14 | 3.1kb | 2024-01-18 18:33:16 | 2024-01-18 18:33:17 |
Judging History
answer
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(...) fprintf (stderr, __VA_ARGS__)
#define lep(i, l, r) for (int i = (l), i##_end = (r); i <= i##_end; ++ i)
#define rep(i, r, l) for (int i = (r), i##_end = (l); i >= i##_end; -- i)
char _c; bool _f; template <class T> inline void IN (T & x) {
_f = 0, x = 0; while (_c = getchar (), ! isdigit (_c)) if (_c == '-') _f = 1;
while (isdigit (_c)) x = x * 10 + _c - '0', _c = getchar (); if (_f) x = - x;
}
const int mod = 998244353;
inline int mul (int x, int y) { return 1ll * x * y % mod; }
inline int qpow (int x, int y, int r = 1) { for ( ; y; y >>= 1, x = mul (x, x)) if (y & 1) r = mul (r, x); return r; }
inline void pls (int &x, int y) { x += y; if (x >= mod) x -= mod; }
inline int add (int x, int y) { x += y; if (x >= mod) x -= mod; return x; }
const int N = 22;
const int NS = (1 << N) | 1;
const int inf = 1e9 + 7;
int n, W, w[N], sw[NS], c[N];
pair <int, int> dp[NS];
inline void trans (int m, int n) {
auto highbit_id = [&] (int s) { return 31 - __builtin_clz (s); };
auto highbit = [&] (int s) { return 1 << highbit_id (s); };
vector <vector <int> > u (1 << n - m);
lep (s, 1, (1 << n - m) - 1) {
for (int _s = s ^ highbit (s), t = _s; ; t = (t - 1) & _s) {
if (sw[s << m] - sw[t << m] <= W) u[t].emplace_back ( s << m );
if (! t) break ;
}
}
vector <vector <int> > v (1 << m);
lep (s, 0, (1 << m) - 1) {
for (int _s = s, t = _s; ; t = (t - 1) & _s) {
if (sw[s] - sw[t] <= W) v[s].emplace_back ( t );
if (! t) break ;
}
}
lep (R, 0, (1 << n - m) - 1) {
sort (u[R].begin (), u[R].end (), [&] (int a, int b) { return sw[a] > sw[b]; });
}
lep (L, 0, (1 << m) - 1) {
sort (v[L].begin (), v[L].end (), [&] (int a, int b) { return sw[a] > sw[b]; });
}
auto upd = [&] (pair <int, int> & f, pair <int, int> & g) {
if (g.first < f.first) f = g;
else if (g.first == f.first) pls (f.second, g.second);
};
lep (R, 0, (1 << n - m) - 1) {
lep (L, 0, (1 << m) - 1) {
int sv = v[L].size (), pv = 0;
pair <int, int> rec = { inf, 0 };
for (auto & _R : u[R]) { // R in _R
int lim = W - (sw[_R] - sw[R << m]);
while (pv < sv && sw[L] - sw[v[L][pv]] <= lim) {
upd (rec, dp[v[L][pv] | (R << m)]), ++ pv;
}
rec.first += c[highbit_id (_R)];
upd (dp[L | _R], rec);
}
}
}
}
signed main () {
IN (n), IN (W);
lep (i, 0, n - 1) IN (w[i]), IN (c[i]);
lep (i, 0, n - 1) {
lep (j, 1, n - 1) {
if (c[j - 1] > c[j]) {
swap (c[j - 1], c[j]);
swap (w[j - 1], w[j]);
}
}
}
lep (i, 0, n - 1) sw[1 << i] = w[i];
lep (i, 1, (1 << n) - 1) sw[i] = sw[i ^ (i & - i)] + sw[i & - i];
lep (s, 1, (1 << n) - 1) dp[s] = { inf, 0 };
dp[0] = { 0, 1 };
function <void (int)> dfs = [&] (int n) {
if (n == 1) {
dp[1].first = c[0];
dp[1].second = 1;
} else {
int m = min (n - 1, (n + 1) / 2);
dfs (m), trans (m, n); // [0, p - 1] -> [0, n - 1]
}
}; dfs (n);
printf ("%d %d\n", dp[(1 << n) - 1].first, dp[(1 << n) - 1].second);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5772kb
input:
5 5 3 5 1 4 2 3 2 2 2 1
output:
9 4
result:
ok 2 number(s): "9 4"
Test #2:
score: -100
Wrong Answer
time: 37ms
memory: 9112kb
input:
18 10000000 956231 904623 1692946 1796774 1081323 1170319 3218792 2542661 3183376 3037270 1869132 1442561 35436 35018 1564635 1939950 1847344 2006043 755870 899310 1671882 2057413 1369264 1338951 3132483 3504034 2056224 1825640 1840949 1562071 1514040 1405352 2300821 2421801 2466540 3004920
output:
9705895 5
result:
wrong answer 1st numbers differ - expected: '9391997', found: '9705895'