| Task: | Yhdistelmät |
| Sender: | jlaire |
| Submission time: | 2025-11-29 23:27:37 +0200 |
| Language: | C++ (C++17) |
| 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.01 s | 1, 4 | details |
| #8 | ACCEPTED | 0.01 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.02 s | 3, 4 | details |
| #13 | ACCEPTED | 0.03 s | 4 | details |
| #14 | ACCEPTED | 0.04 s | 4 | details |
| #15 | ACCEPTED | 0.08 s | 4 | details |
| #16 | ACCEPTED | 0.12 s | 4 | details |
| #17 | ACCEPTED | 0.15 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.39 s | 4 | details |
| #21 | ACCEPTED | 0.22 s | 4 | details |
Code
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <random>
#include <set>
#include <vector>
using namespace std;
namespace {
// Source: https://github.com/jaehyunp/stanfordacm/blob/master/code/Simplex.cc
// Two-phase simplex algorithm for solving linear programs of the form
//
// maximize c^T x
// subject to Ax <= b
// x >= 0
//
// INPUT: A -- an m x n matrix
// b -- an m-dimensional vector
// c -- an n-dimensional vector
// x -- a vector where the optimal solution will be stored
//
// OUTPUT: value of the optimal solution (infinity if unbounded
// above, nan if infeasible)
//
// To use this code, create an LPSolver object with A, b, and c as
// arguments. Then, call Solve(x).
//typedef long double DOUBLE;
typedef double DOUBLE;
typedef vector<DOUBLE> VD;
typedef vector<VD> VVD;
typedef vector<int> VI;
const DOUBLE EPS = 1e-9;
struct LPSolver {
int m, n;
VI N, B;
VVD D;
LPSolver(const VVD &A, const VD &b, const VD &c) :
m(b.size()), n(c.size()), N(n + 1), B(m), D(m + 2, VD(n + 2)) {
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) D[i][j] = A[i][j];
for (int i = 0; i < m; i++) { B[i] = n + i; D[i][n] = -1; D[i][n + 1] = b[i]; }
for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; }
N[n] = -1; D[m + 1][n] = 1;
}
void Pivot(int r, int s) {
double inv = 1.0 / D[r][s];
for (int i = 0; i < m + 2; i++) if (i != r)
for (int j = 0; j < n + 2; j++) if (j != s)
D[i][j] -= D[r][j] * D[i][s] * inv;
for (int j = 0; j < n + 2; j++) if (j != s) D[r][j] *= inv;
for (int i = 0; i < m + 2; i++) if (i != r) D[i][s] *= -inv;
D[r][s] = inv;
swap(B[r], N[s]);
}
bool Simplex(int phase) {
int x = phase == 1 ? m + 1 : m;
while (true) {
int s = -1;
for (int j = 0; j <= n; j++) {
if (phase == 2 && N[j] == -1) continue;
if (s == -1 || D[x][j] < D[x][s] || (D[x][j] == D[x][s] && N[j] < N[s])) s = j;
}
if (D[x][s] > -EPS) return true;
int r = -1;
for (int i = 0; i < m; i++) {
if (D[i][s] < EPS) continue;
if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] ||
((D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r])) r = i;
}
if (r == -1) return false;
Pivot(r, s);
}
}
DOUBLE Solve(VD &x) {
int r = 0;
for (int i = 1; i < m; i++) if (D[i][n + 1] < D[r][n + 1]) r = i;
if (D[r][n + 1] < -EPS) {
Pivot(r, n);
if (!Simplex(1) || D[m + 1][n + 1] < -EPS) return -numeric_limits<DOUBLE>::infinity();
for (int i = 0; i < m; i++) if (B[i] == -1) {
int s = -1;
for (int j = 0; j <= n; j++)
if (s == -1 || D[i][j] < D[i][s] || (D[i][j] == D[i][s] && N[j] < N[s])) s = j;
Pivot(i, s);
}
}
if (!Simplex(2)) return numeric_limits<DOUBLE>::infinity();
x = VD(n);
for (int i = 0; i < m; i++) if (B[i] < n) x[B[i]] = D[i][n + 1];
return D[m][n + 1];
}
};
}
struct Problem {
vector<int> P;
vector<int> X;
vector<vector<int>> groups;
};
vector<int> solve(Problem prob) {
const auto& P = prob.P;
const auto& X = prob.X;
const auto& groups = prob.groups;
int n = P.size();
int m = X.size();
VVD A;
for (int i=0; i<n; i++) {
VD row(n+m);
row[i] = 1;
A.push_back(move(row));
}
for (int i=0; i<m; i++) {
for (int id:groups[i]) {
VD row(n+m);
row[n+i] = 1;
row[id] = -1;
A.push_back(move(row));
}
}
VD b(A.size());
for (int i=0; i<n; i++) b[i] = 1;
VD c(n+m);
for (int i=0; i<n; i++) c[i] = -P[i] + 1e-4;
for (int i=0; i<m; i++) c[n+i] = X[i];
LPSolver solver(A, b, c);
VD x;
solver.Solve(x);
vector<int> ans;
for (int i=0; i<n; i++) {
if (x[i] > 0.5) {
ans.push_back(i);
}
}
return ans;
}
int get_value(const Problem& prob, const vector<int>& ans) {
int value = 0;
for (int id : ans) {
value -= prob.P[id];
}
set<int> S(ans.begin(), ans.end());
for (int i=0; i<(int)prob.X.size(); i++) {
bool ok = true;
for (int id : prob.groups[i]) {
ok &= S.count(id);
}
if (ok) {
value += prob.X[i];
}
}
return value;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n,m; cin>>n>>m;
vector<int> P(n); for (int&x:P) cin>>x;
vector<int> X(m);
vector<vector<int>> groups(m);
for (int i=0; i<m; i++) {
int k; cin>>k>>X[i];
groups[i].resize(k);
for (int& id:groups[i]) cin>>id, id--;
}
Problem prob{P, X, groups};
vector<int> ans = solve(prob);
int sum = get_value(prob, ans);
int len = ans.size();
cout << sum << '\n';
cout << len << '\n';
for (int i=0; i<len; i++) {
cout << (ans[i]+1) << " \n"[i+1==len];
}
}
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 ... |
