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

    传送门

    题目要求的就是求一个线性无关的方程组……

    于是学到了一个新的东西,叫实数下的线性基……这个其实和普通的线性基很像。具体做法就是,我们首先把所有物品按价格从小到大排序,之后贪心的先选取价格小的。每次我们遍历其一位属性,如果这一维基底当前为空,就把它压入,否则的话计算它和当前这一维基底的比值div,并且把后面多维的属性减去(div*g[p[j]].a[k]),也就是当前基底的剩下多维属性。

    这个地方其实就相当于我们在线性基的时候进行异或。因为我们是要看这些基底能否组成它,所以就这样进行一个类似高斯消元的操作,如果这个装备各维属性全部变成了0,说明他能被当前基底组合,也就不需要购买啦。

    然后有人说这题卡精度……我的eps是1e-5,反正是一次过了……

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #define rep(i,a,n) for(register int i = a;i <= n;i++)
    #define per(i,n,a) for(register int i = n;i >= a;i--)
    #define enter putchar('
    ')
    #define pr pair<int,int>
    #define mp make_pair
    #define fi first
    #define sc second
    #define I inline
    using namespace std;
    typedef long long ll;
    const int M = 20005;
    const int N = 10000005;
    const double eps = 1e-5;
    
     
    int read()
    {
       int ans = 0,op = 1;char ch = getchar();
       while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
       while(ch >='0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
       return ans * op;
    }
    
    struct node
    {
       double a[505];
       int w;
       bool operator < (const node &g) const {return w < g.w;} 
    }g[505];
    
    int n,m,tot,sum,p[505];
    
    int main()
    {
       n = read(),m = read();
       rep(i,1,n)
          rep(j,1,m) scanf("%lf",&g[i].a[j]);
       rep(i,1,n) g[i].w = read();
       sort(g+1,g+1+n);
       rep(i,1,n)
          rep(j,1,m)
       {
          if(fabs(g[i].a[j]) < eps) continue;
          if(!p[j]) {p[j] = i,tot++,sum += g[i].w;break;}
          double div = g[i].a[j] / g[p[j]].a[j];
          rep(k,j,m) g[i].a[k] -= div * g[p[j]].a[k];
       }
       printf("%d %d
    ",tot,sum);
       return 0;
    }
    
    
  • 相关阅读:
    Autho2----完整搭建实例
    详解SpringBoot应用跨域访问解决方案
    微信小程序后端开发流程
    前端必备 Nginx 配置
    后端必备 Nginx 配置
    关于spring boot集成MQTT
    Java 常用IO流操作详解
    spring boot 整合mybatis 的xml版本【包括逆向工程以及分页插件】
    实用 SQL 语句
    整理收集的一些常用java工具类
  • 原文地址:https://www.cnblogs.com/captain1/p/10246862.html
Copyright © 2011-2022 走看看