[JLOI2015]装备购买(线性基)

https://ac.nowcoder.com/acm/problem/20146

先排序,然后一个个往里面插就可以,这里使用的实数域的线性基。

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const double eps=1e-6;
struct node{
    long double val[555];
    int w;
    bool operator <(const node &t)const{
        return w<t.w;
    }
}a[555];
int p[555];


int main()
{
    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        cin>>a[i].val[j];
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i].w);

    sort(a+1,a+n+1);

    int cnt=0,ans=0;
    memset(p,0,sizeof(p));
    /* 实数域线性基   */
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            //cout<<i<<","<<j<<":"<<a[i].val[j]<<endl;
            if(fabs(a[i].val[j])<eps) continue;
            if(!p[j]){
                cnt++,ans+=a[i].w;
                p[j]=i;
                break;
            }
            else{
                long double tmp=a[i].val[j]/a[p[j]].val[j];
                for(int k=j;k<=m;k++){
                    a[i].val[k]-=a[p[j]].val[k]*tmp;
                }
            }
        }
    }
    cout<<cnt<<' '<<ans<<endl;
}

 

posted @ 2020-12-16 10:26  Npunchman  阅读(88)  评论(0)    收藏  举报