Bzoj4004: [JLOI2015]装备购买

题面

传送门

Sol

很像线性基

其实就是线性基
我一直以为它只能搞异或
线性基解决异或问题时是怎么插入的?
就像高斯消元一样,如果这里有值,就进行消元
否则直接加入

那么这个题也可以如此,只是把异或改成减法
这样我们就把高斯消元和线性基结合起来了

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
typedef long double lod;

template <class Int>
IL void Input(RG Int &x){
    RG int z = 1; RG char c = getchar(); x = 0;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= z;
}

const int maxn(505);
const lod EPS(1e-5);

int n, m, c[maxn], id[maxn], ans1, ans2, vis[maxn];
lod a[maxn][maxn], w[maxn][maxn];

IL int Cmp(RG int x, RG int y){
    return c[x] < c[y];
}

IL int Check(RG int x){
    for(RG int i = 1; i <= m; ++i)
        if(fabs(w[x][i]) > EPS){
            if(!vis[i]){
                vis[i] = 1;
                for(RG int j = 1; j <= m; ++j) a[i][j] = w[x][j];
                return 1;
            }
            else{
                RG lod div = w[x][i] / a[i][i];
                for(RG int j = i; j <= m; ++j) w[x][j] -= a[i][j] * div;
            }
        }
    return 0;
}

int main(RG int argc, RG char* argv[]){
    Input(n), Input(m);
    for(RG int i = 1; i <= n; ++i)
        for(RG int j = 1, t; j <= m; ++j)
            Input(t), w[i][j] = t;
    for(RG int i = 1; i <= n; ++i) Input(c[i]), id[i] = i;
    sort(id + 1, id + n + 1, Cmp);
    for(RG int p = 1, i; p <= n; ++p)
        if(Check(i = id[p])) ++ans1, ans2 += c[i];
    printf("%d %d\n", ans1, ans2);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值