Luogu P3265 [JLOI2015]装备购买

题目描述

https://www.luogu.com.cn/problem/P3265

Solution

我们考虑将装备按照权值排序然后插入到线性基中

贪心选择即可,贪心的正确性可以用拟阵证明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define maxn 510
using namespace std;

const double eps = 1e-5;

int n, m, p[maxn], w[maxn], id[maxn];

double a[maxn][maxn];

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> a[i][j];
for (int i = 1; i <= n; ++i) cin >> w[i];
for (int i = 1; i <= n; ++i) id[i] = i;
sort(id + 1, id + n + 1, [](const int &u, const int &v) { return w[u] < w[v]; });
int ans = 0, sum = 0;
for (int o = 1, i = id[1]; o <= n; i = id[++o])
for (int j = 1; j <= m; ++j) {
if (fabs(a[i][j]) < eps) continue;
if (!p[j]) { p[j] = i, ++ans, sum += w[i]; break; }
double t = a[i][j] / a[p[j]][j];
for (int k = j; k <= m; ++k) a[i][k] -= t * a[p[j]][k];
}
cout << ans << " " << sum << "\n";
return 0;
}