zoukankan      html  css  js  c++  java
  • 【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('
    ')
    #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();
    }
    
  • 相关阅读:
    Luogu P4316 绿豆蛙的归宿 题解报告
    Luogu P1850 换教室(NOIP 2016) 题解报告
    Rainbow的信号 题解报告
    $CH5105 Cookies$ 线性$DP+$贪心
    算法竞赛 $0×50$ 动态规划 (+一本通
    $CH5104 I-country$ 线性$DP$
    洛谷$2014$ 选课 背包类树形$DP$
    $SP703 Mobile Service DP$
    $POJ1015 Jury Compromise Dp$/背包
    $POJ1742 Coins$ 多重背包+贪心
  • 原文地址:https://www.cnblogs.com/ivorysi/p/9600516.html
Copyright © 2011-2022 走看看