zoukankan      html  css  js  c++  java
  • bzoj4004 [JLOI2015]装备购买

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4004

    【题解】

    这种题目怎么这么眼熟呢?似乎这样的都能贪心。

    按照物品价格从小到大排序,然后贪心插入。

    插入的时候消元即可(线性基思想)

    卡精度啊qwq

    # include <math.h>
    # include <stdio.h>
    # include <string.h>
    # include <algorithm>
    // # include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    const int M = 5e2 + 10;
    const int mod = 1e9+7;
    
    # define RG register
    # define ST static
    
    int n, m;
    
    struct pa {
        ld a[M];
        int v;
        friend bool operator<(pa a, pa b) {
            return a.v<b.v;
        }
    }p[M]; 
    
    int cur[M]; 
    int u, ans;
    
    int main() {
        scanf("%d%d", &n, &m);
        for (int i=1; i<=n; ++i) 
            for (int j=1; j<=m; ++j) {
                int t; scanf("%d", &t);
                p[i].a[j] = (ld)t;
            }
        for (int i=1; i<=n; ++i) scanf("%d", &p[i].v); 
        sort(p+1, p+n+1); 
        for (int i=1; i<=n; ++i) {
            for (int j=1; j<=m; ++j) {
                if(fabs(p[i].a[j]) < 1e-8) continue; 
                if(cur[j]) {
                    ld r = p[i].a[j]/p[cur[j]].a[j];
                    for (int k=j; k<=m; ++k) p[i].a[k] -= r*p[cur[j]].a[k];
                } else {cur[j] = i; ans += p[i].v; ++u;break;} 
            }
        }
        printf("%d %d
    ", u, ans); 
        return 0;
    }
    View Code
  • 相关阅读:
    CodeForces
    EOJ 3506. 斐波那契数列
    牛客练习赛13 D幸运数字Ⅳ . 康托逆展开
    UVA
    Piggy-Bank HDU
    Dollar Dayz POJ
    UVA 674 Coin Change (完全背包)
    python OOP (1)
    python lambda简易使用
    python whl模块安装方法
  • 原文地址:https://www.cnblogs.com/galaxies/p/bzoj4004.html
Copyright © 2011-2022 走看看