| Task: | Yhdistelmät |
| Sender: | Metabolix |
| Submission time: | 2025-12-04 12:28:21 +0200 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 16 |
| #2 | ACCEPTED | 17 |
| #3 | ACCEPTED | 29 |
| #4 | ACCEPTED | 38 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | 1, 2, 3, 4 | details |
| #2 | ACCEPTED | 0.00 s | 1, 3, 4 | details |
| #3 | ACCEPTED | 0.00 s | 1, 4 | details |
| #4 | ACCEPTED | 0.00 s | 1, 4 | details |
| #5 | ACCEPTED | 0.00 s | 1, 4 | details |
| #6 | ACCEPTED | 0.00 s | 1, 4 | details |
| #7 | ACCEPTED | 0.00 s | 1, 4 | details |
| #8 | ACCEPTED | 0.00 s | 1, 4 | details |
| #9 | ACCEPTED | 0.00 s | 1, 4 | details |
| #10 | ACCEPTED | 0.00 s | 1, 4 | details |
| #11 | ACCEPTED | 0.01 s | 2, 3, 4 | details |
| #12 | ACCEPTED | 0.01 s | 3, 4 | details |
| #13 | ACCEPTED | 0.01 s | 4 | details |
| #14 | ACCEPTED | 0.01 s | 4 | details |
| #15 | ACCEPTED | 0.02 s | 4 | details |
| #16 | ACCEPTED | 0.02 s | 4 | details |
| #17 | ACCEPTED | 0.02 s | 4 | details |
| #18 | ACCEPTED | 0.00 s | 1, 2, 3, 4 | details |
| #19 | ACCEPTED | 0.00 s | 1, 2, 3, 4 | details |
| #20 | ACCEPTED | 0.01 s | 4 | details |
| #21 | ACCEPTED | 0.01 s | 4 | details |
Code
#include <bits/stdc++.h>
using namespace std;
//typedef bitset<128> bits_t;
struct bits_t {
__uint128_t data;
bits_t() : data(0) {}
bits_t(__uint128_t d) : data(d) {}
void set(int i, bool value = true) {
if (!value) {
data &= ~(__uint128_t(1) << i);
return;
}
data |= (__uint128_t(1) << i);
}
bool test(int i) const {
return (data & (__uint128_t(1) << i)) != 0;
}
int count() const {
return __builtin_popcountll(uint64_t(data)) + __builtin_popcountll(uint64_t(data >> 64));
}
bits_t operator&(const bits_t& other) const {
return bits_t(data & other.data);
}
bits_t operator|(const bits_t& other) const {
return bits_t(data | other.data);
}
bits_t operator~() const {
return bits_t(~data);
}
bool operator==(const bits_t& other) const {
return data == other.data;
}
bool operator!=(const bits_t& other) const {
return data != other.data;
}
bool operator<(const bits_t& other) const {
return data < other.data;
}
};
namespace std {
template <>
struct hash<bits_t> {
size_t operator()(const bits_t& b) const {
return uint64_t(b.data) ^ uint64_t(b.data >> 50);
}
};
}
int lowest_set_bit(bits_t b) {
if (b.data == 0) {
return -1;
}
if ((uint64_t)b.data != 0) {
return __builtin_ctzll(uint64_t(b.data));
}
return __builtin_ctzll(uint64_t(b.data >> 64)) + 64;
}
typedef unordered_map<bits_t, long> palkkiot_t;
typedef vector<long> hinnat_t;
void lue_syote(hinnat_t& hinnat, palkkiot_t& palkkiot) {
int n, m;
cin >> n >> m;
hinnat.resize(n);
for (int i = 0; i < n; ++i) {
cin >> hinnat[i];
}
palkkiot.clear();
for (int i = 0; i < m; ++i) {
bits_t yhdistelmä;
int k, x;
cin >> k >> x;
for (int j = 0; j < k; ++j) {
int indeksi;
cin >> indeksi;
indeksi -= 1;
yhdistelmä.set(indeksi);
}
palkkiot[yhdistelmä] += x;
}
}
void luo_syote(hinnat_t& hinnat, palkkiot_t& palkkiot, int seed) {
srand(seed);
int n = 20, m = n + rand() % 20 + 1;
hinnat.resize(n);
for (int i = 0; i < n; ++i) {
hinnat[i] = rand() % 1000 + 1;
}
palkkiot.clear();
for (int i = 0; i < m; ++i) {
bits_t yhdistelmä;
int k = rand() % n + 1;
for (int j = 0; j < k; ++j) {
int indeksi = rand() % n;
yhdistelmä.set(indeksi);
}
int x = rand() % 1000 + 1;
palkkiot[yhdistelmä] += x;
}
}
void ratkaise(hinnat_t hinnat, palkkiot_t palkkiot, bool testi) {
const int n = hinnat.size();
auto laske_arvo = [&](bits_t yhdistelmä) {
int arvo = 0;
for (int i = 0; i < n; ++i) {
if (yhdistelmä.test(i)) {
arvo -= hinnat[i];
}
}
for (const auto& [palkkio_yhdistelmä, palkkio] : palkkiot) {
if ((yhdistelmä & palkkio_yhdistelmä) == palkkio_yhdistelmä) {
arvo += palkkio;
}
}
return arvo;
};
auto ilmoita_tulos = [&](bits_t yhdistelmä) {
cout << laske_arvo(yhdistelmä) << "\n";
cout << yhdistelmä.count() << "\n";
for (int i = 0; i < n; ++i) {
if (yhdistelmä.test(i)) {
cout << (i + 1) << " ";
}
}
cout << endl;
};
bits_t alku, loppu;
loppu.set(n);
unordered_map<bits_t, unordered_map<bits_t, long>> kapasiteetit;
for (const auto& [yhdistelmä, palkkio] : palkkiot) {
kapasiteetit[yhdistelmä][loppu] = palkkio;
kapasiteetit[loppu][yhdistelmä] = 0;
}
for (int i = 0; i < n; ++i) {
bits_t solmu;
solmu.set(n);
solmu.set(i);
kapasiteetit[alku][solmu] = hinnat[i];
kapasiteetit[solmu][alku] = 0;
for (const auto& [yhdistelmä, palkkio] : palkkiot) {
if (yhdistelmä.test(i)) {
kapasiteetit[solmu][yhdistelmä] = 1000000;
kapasiteetit[yhdistelmä][solmu] = 0;
}
}
}
// Virtauslaskenta alku - loppu.
bits_t ratkaisu;
long max_virtaus = 0;
while (true) {
unordered_map<bits_t, bits_t> edeltäjät;
unordered_map<bits_t, long> virta;
queue<bits_t> q;
q.push(alku);
virta[alku] = LONG_MAX;
while (!q.empty()) {
bits_t nykyinen = q.front();
q.pop();
for (const auto& [seuraava, kapasiteetti] : kapasiteetit[nykyinen]) {
if (kapasiteetti > 0 && virta.find(seuraava) == virta.end()) {
edeltäjät[seuraava] = nykyinen;
virta[seuraava] = min(virta[nykyinen], kapasiteetti);
if (seuraava == loppu) {
goto ulos;
}
q.push(seuraava);
}
}
}
ulos:
if (virta.find(loppu) == virta.end()) {
break;
}
long lisäys = virta[loppu];
max_virtaus += lisäys;
bits_t nykyinen = loppu;
while (nykyinen != alku) {
bits_t edeltäjä = edeltäjät[nykyinen];
kapasiteetit[edeltäjä][nykyinen] -= lisäys;
kapasiteetit[nykyinen][edeltäjä] += lisäys;
nykyinen = edeltäjä;
}
}
unordered_set<bits_t> käyty;
queue<bits_t> q;
q.push(alku);
käyty.insert(alku);
while (!q.empty()) {
bits_t nykyinen = q.front();
q.pop();
for (const auto& [seuraava, kapasiteetti] : kapasiteetit[nykyinen]) {
if (kapasiteetti > 0 && käyty.find(seuraava) == käyty.end()) {
käyty.insert(seuraava);
q.push(seuraava);
}
}
}
for (const auto& [yhdistelmä, _] : palkkiot) {
if (käyty.find(yhdistelmä) == käyty.end()) {
ratkaisu = ratkaisu | yhdistelmä;
}
}
auto pieni_tapaus = [&]() {
bits_t paras_yhdistelmä = 0;
int paras_arvo = 0;
for (uint32_t yhdistelmä = 0; yhdistelmä < (uint32_t(1) << n); ++yhdistelmä) {
bits_t bs_yhdistelmä(yhdistelmä);
int arvo = laske_arvo(bs_yhdistelmä);
if (arvo > paras_arvo) {
paras_yhdistelmä = bs_yhdistelmä;
paras_arvo = arvo;
}
}
return paras_yhdistelmä;
};
if (testi) {
bits_t oikea_yhdistelmä = pieni_tapaus();
if (ratkaisu != oikea_yhdistelmä) {
ilmoita_tulos(oikea_yhdistelmä);
cout << "----\n";
ilmoita_tulos(ratkaisu);
throw runtime_error("VIRHE");
}
return;
}
ilmoita_tulos(ratkaisu);
}
int main(int argc, char** argv) {
hinnat_t hinnat;
palkkiot_t palkkiot;
if (argc == 2 && string(argv[1]) == "kotitesti") {
try {
lue_syote(hinnat, palkkiot);
ratkaise(hinnat, palkkiot, true);
for (int seed = 1; seed <= 1000; ++seed) {
cerr << "Testi " << seed << "\n";
luo_syote(hinnat, palkkiot, seed);
ratkaise(hinnat, palkkiot, true);
}
} catch (runtime_error& e) {
cerr << e.what() << "\n";
}
} else {
lue_syote(hinnat, palkkiot);
ratkaise(hinnat, palkkiot, false);
}
}
Test details
Test 1
Group: 1, 2, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 80 69 91 47 74 75 94 22 100 43... |
| correct output |
|---|
| 446 11 2 3 5 8 10 13 14 15 17 19 20 |
| user output |
|---|
| 446 11 2 3 5 8 10 13 14 15 17 19 20 |
Test 2
Group: 1, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 5 42 7 18 55 64 64 83 73 44 22... |
| correct output |
|---|
| 425 11 1 2 3 4 7 10 13 16 18 19 20 |
| user output |
|---|
| 425 11 1 2 3 4 7 10 13 16 18 19 20 |
Test 3
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 30 98 55 69 40 3 95 12 64 56 3... |
| correct output |
|---|
| 284 8 1 3 8 10 15 16 18 19 |
| user output |
|---|
| 284 8 1 3 8 10 15 16 18 19 |
Test 4
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 11 44 58 8 16 52 20 43 24 31 4... |
| correct output |
|---|
| 348 10 2 4 5 7 11 13 15 16 18 20 |
| user output |
|---|
| 348 10 2 4 5 7 11 13 15 16 18 20 |
Test 5
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 53 44 5 37 88 36 81 47 85 97 3... |
| correct output |
|---|
| 119 13 1 2 4 8 10 11 13 14 16 17 18 1... |
| user output |
|---|
| 119 13 1 2 4 8 10 11 13 14 16 17 18 1... |
Test 6
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 20 27 75 94 48 62 37 55 49 67 ... |
| correct output |
|---|
| 478 11 1 2 5 7 10 12 13 15 16 17 18 |
| user output |
|---|
| 478 11 1 2 5 7 10 12 13 15 16 17 18 |
Test 7
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 32 28 67 72 32 76 53 30 47 67 ... |
| correct output |
|---|
| 215 10 2 4 5 7 8 9 11 13 16 20 |
| user output |
|---|
| 215 10 2 4 5 7 8 9 11 13 16 20 |
Test 8
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 39 72 74 79 49 45 73 44 37 4 7... |
| correct output |
|---|
| 185 13 1 3 5 6 7 8 10 13 14 16 17 18 ... |
| user output |
|---|
| 185 13 1 3 5 6 7 8 10 13 14 16 17 18 ... |
Test 9
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 41 56 65 78 2 13 17 42 83 76 9... |
| correct output |
|---|
| 95 11 2 4 5 6 7 8 13 14 17 18 19 |
| user output |
|---|
| 95 11 2 4 5 6 7 8 13 14 17 18 19 |
Test 10
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 20 20 43 1 20 61 25 46 2 18 36 1 85 ... |
| correct output |
|---|
| 111 8 1 2 3 7 8 10 16 19 |
| user output |
|---|
| 111 8 1 2 3 7 8 10 16 19 |
Test 11
Group: 2, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 992248 852673 366775 737068 56... |
| correct output |
|---|
| 30642743 46 3 5 6 9 11 12 15 16 17 18 21 2... |
| user output |
|---|
| 30642743 46 3 5 6 9 11 12 15 16 17 18 21 2... |
Test 12
Group: 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 153790 361741 45017 47184 9422... |
| correct output |
|---|
| 16529629 39 2 3 4 5 10 12 14 17 24 25 26 3... |
| user output |
|---|
| 16529629 39 2 3 4 5 10 12 14 17 24 25 26 3... |
Test 13
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 186797 446409 957173 150683 17... |
| correct output |
|---|
| 14928280 62 1 2 8 9 10 11 12 14 15 16 17 2... |
| user output |
|---|
| 14928280 62 1 2 8 9 10 11 12 14 15 16 17 2... |
Test 14
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 343213 582494 707357 104682 66... |
| correct output |
|---|
| 11308944 72 1 3 4 5 6 7 8 9 10 11 12 13 14... |
| user output |
|---|
| 11308944 72 1 3 4 5 6 7 8 9 10 11 12 13 14... |
Test 15
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 922546 12088 805566 351521 644... |
| correct output |
|---|
| 3311952 10 14 17 26 29 64 65 70 76 83 95 |
| user output |
|---|
| 3311952 10 14 17 26 29 64 65 70 76 83 95 |
Test 16
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 923042 35929 531316 587665 845... |
| correct output |
|---|
| 519209 6 2 18 45 61 64 86 |
| user output |
|---|
| 519209 6 2 18 45 61 64 86 |
Test 17
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 493725 218022 417464 531537 83... |
| correct output |
|---|
| 1255541 11 16 19 24 29 30 50 60 62 67 74 ... |
| user output |
|---|
| 1255541 11 16 19 24 29 30 50 60 62 67 74 ... |
Test 18
Group: 1, 2, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 1 1 2 1 1 1 |
| correct output |
|---|
| 0 0 |
| user output |
|---|
| 0 0 |
Test 19
Group: 1, 2, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 1 1 1 1 2 1 |
| correct output |
|---|
| 1 1 1 |
| user output |
|---|
| 1 1 1 |
Test 20
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 1000000 1000000 1000000 100000... |
| correct output |
|---|
| 0 100 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
| user output |
|---|
| 0 100 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
Test 21
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| correct output |
|---|
| 99999900 100 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
| user output |
|---|
| 99999900 100 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
