【LOJ】#2108. 「JLOI2015」装备购买
题解
换成long double才过……出题人丧心病狂卡精度
只要按照费用排序从小到大排序,一个个插入线性基,插入的时候加上费用即可
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
using namespace std;
typedef long long int64;
typedef long double db;
typedef unsigned int u32;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {putchar('-');x = -x;}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int N,M;
db num[505][505];
int id[505],c[505],wh[505],cnt;
int64 ans;
bool cmp(int a,int b) {
return c[a] < c[b];
}
void Init() {
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= M ; ++j) {
scanf("%Lf",&num[i][j]);
}
}
for(int i = 1 ; i <= N ; ++i) {read(c[i]);id[i] = i;}
sort(id + 1,id + N + 1,cmp);
}
void Solve() {
for(int i = 1 ; i <= N ; ++i) {
int u = id[i];
for(int j = M ; j >= 1 ; --j) {
if(fabs(num[u][j]) > 1e-8) {
if(wh[j]) {
for(int k = 1 ; k <= j ; ++k) num[u][k] -= num[u][j] * num[wh[j]][k];
}
else {
for(int k = 1 ; k <= j ; ++k) num[u][k] /= num[u][j];
wh[j] = u;ans += c[u];++cnt;break;
}
}
}
}
out(cnt);space;out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}