QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#308446 | #8005. Crossing the Border | hos_lyric | ML | 1245ms | 296884kb | C++14 | 3.9kb | 2024-01-20 05:11:46 | 2024-01-20 05:11:46 |
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")
constexpr int MO = 998244353;
constexpr int MAX_N = 22;
int N, W;
vector<pair<int, int>> P;
vector<int> CSum, WSum;
template <class T> void zeta(T zs, int n, int m, int h0) {
const int pop0 = __builtin_popcount(h0);
if (m) {
--m;
for (int h = h0; h < h0 + (1 << m); ++h) {
const int pop = __builtin_popcount(h);
for (int k = pop - pop0; k <= pop; ++k) zs[h | 1 << m][k] += zs[h][k];
}
zeta(zs, n, m, h0);
zeta(zs, n, m, h0 | 1 << m);
}
}
template <class T> void init(T zs, int n) {
for (int p = 0; p < 1 << n; ++p) {
fill(zs[p], zs[p] + (n + 1), 0);
if (WSum[p | 1 << n] <= W) {
zs[p][__builtin_popcount(p)] = 1;
}
}
zeta(zs, n, n, 0);
}
/*
zas: ways to have ...
after processing [i, N),
mask for [0, i): used items
mask for [i, N-1): saved costs
keep:
ranked poly
zeta'ed for [0, i)
*/
Int zas[1 << (MAX_N - 1)][(MAX_N - 1) + 1];
Int zbs[1 << (MAX_N - 2)][(MAX_N - 2) + 1];
int main() {
for (; ~scanf("%d%d", &N, &W); ) {
P.resize(N);
for (int i = 0; i < N; ++i) {
scanf("%d%d", &P[i].second, &P[i].first);
}
sort(P.begin(), P.end());
CSum.assign(1 << N, 0);
WSum.assign(1 << N, 0);
for (int i = 0; i < N; ++i) {
for (int h = 0; h < 1 << i; ++h) {
CSum[h | 1 << i] = CSum[h] + P[i].first;
WSum[h | 1 << i] = WSum[h] + P[i].second;
}
}
init(zas, N - 1);
for (int i = N - 1; --i >= 0; ) {
init(zbs, i);
for (int q = 0; q < 1 << (N - 1); q += (1 << (i + 1))) {
/*
current: (0, 0+1)
0: convolve
1: save
*/
for (int p = 0; p < 1 << i; ++p) {
const int h0 = p | q;
const int h1 = p | 1 << i | q;
for (int k = 0; k <= N - 1; ++k) {
zas[h1][k] -= zas[h0][k];
}
for (int k = N - 1; k >= 0; --k) for (int l = 1; l <= i && k + l <= N - 1; ++l) {
zas[h0][k + l] += zas[h0][k] * zbs[p][l];
}
}
}
// cerr<<"DONE i = "<<i<<endl;
// for(int p=0;p<1<<(N-1);++p){cerr<<"zas["<<p<<"] = ";pv(zas[p],zas[p]+N);}
}
vector<Int> ways(1 << (N - 1));
for (int h = 0; h < 1 << (N - 1); ++h) {
ways[h] = zas[h][__builtin_popcount(h)];
}
// cerr<<"ways = "<<ways<<endl;
int ans = CSum.back() + 1;
Int way = 0;
for (int h = 0; h < 1 << (N - 1); ++h) if (ways[h]) {
const int cost = CSum.back() - CSum[h];
if (ans > cost) {
ans = cost;
way = ways[h];
} else if (ans == cost) {
way += ways[h];
}
}
printf("%d %lld\n", ans, way);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5844kb
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: 0
Accepted
time: 90ms
memory: 42384kb
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:
9391997 70
result:
ok 2 number(s): "9391997 70"
Test #3:
score: 0
Accepted
time: 531ms
memory: 152104kb
input:
20 10000000 1289384 1416015 1692778 1966748 1747794 1708650 2885387 2925290 2516650 2410838 2202363 2092667 368691 407497 1897764 1902790 180541 224758 1089173 1075924 2005212 1743637 702568 566295 465783 369143 2722863 2902398 174068 150211 513930 519657 1634023 1313239 1133070 1040937 961394 11066...
output:
6331196 89
result:
ok 2 number(s): "6331196 89"
Test #4:
score: 0
Accepted
time: 1245ms
memory: 296884kb
input:
21 10000000 1432782 1230128 1693282 1456826 605524 521515 2742745 3427204 2231114 2129928 2345527 2397808 511783 521160 2041234 2313965 2323807 2603481 1232121 1410811 719508 850004 416942 495559 2180169 2579591 1580089 1786914 2317568 2292171 1514260 1143717 1348703 1495001 562052 525544 2818854 23...
output:
9336572 5
result:
ok 2 number(s): "9336572 5"
Test #5:
score: -100
Memory Limit Exceeded
input:
22 10000000 1562592 1176882 1693226 1513484 2293770 2757728 2612851 3010518 1971354 2392268 2475363 2035487 641627 684375 2171036 2181775 1544541 1633457 1361981 1060447 2277948 2792254 157192 141039 1011327 1139897 541119 577682 1538276 1451191 2423314 2061841 1088919 1154927 42526 43789 1779858 16...
output:
8019829 516