zoukankan      html  css  js  c++  java
  • BZOJ 4004 [JLOI2015]装备购买 | 线性基

    题目链接

    Luogu P3265

    题解

    非常正常的线性基!

    但是我不会线性基……

    (吐槽:#define double long double 才过……)

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #define space putchar(' ')
    #define enter putchar('
    ')
    typedef long long ll;
    using namespace std;
    template <class T>
    void read(T &x){
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
    	if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
    	x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x){
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    
    #define double long double
    
    const int N = 505;
    const double eps = 1e-5;
    int n, m, ins[N], ans, cnt;
    struct vec {
        int val;
        double x[N];
        bool operator < (const vec &b) const{
    	return val < b.val;
        }
    } a[N];
    
    void gauss(){
        for(int i = 1; i <= n; i++)
    	for(int j = 1; j <= m; j++)
    	    if(fabs(a[i].x[j]) > eps){
    		if(!ins[j]){
    		    cnt++, ans += a[i].val;
    		    ins[j] = i;
    		    break;
    		}
    		double rate = a[i].x[j] / a[ins[j]].x[j];
    		for(int k = m; k >= j; k--)
    		    a[i].x[k] -= rate * a[ins[j]].x[k];
    	    }
    }
    
    int main(){
    
        read(n), read(m);
        for(int i = 1; i <= n; i++)
    	for(int j = 1; j <= m; j++)
    	    read(a[i].x[j]);
        for(int i = 1; i <= n; i++)
    	read(a[i].val);
        sort(a + 1, a + n + 1);
        gauss();
        write(cnt), space, write(ans), enter;
    
        return 0;
    }
    
    
  • 相关阅读:
    什么是X86和X86-64
    PyQt 5 的学习引言
    GraphQL实战篇(一)
    GraphQL基础篇
    .net之设计模式
    .net面向对象设计原则
    Entity Framework (EF) Core工具创建一对多和多对多的关系
    NLog组件
    给dao层注入jdbcTemplate时的一个强行bug(jdbcDaoSupport不要随便用!用了要记得!)
    使用工厂模式解耦和IoC思想
  • 原文地址:https://www.cnblogs.com/RabbitHu/p/BZOJ4004.html
Copyright © 2011-2022 走看看