线性基

通过题目描述可以感觉到就是要求线性基,

线性基的求法是高斯消元,消完以后剩下的x的系数非 0 的就是线性基

本题有一个贪心策略,每次挑选价格最小的来消掉其他的元

//可以快排预处理
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN = 505;
int b[MAXN], n, m;
long double a[MAXN][MAXN];
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for(int i = 1; i <= n; i++) cin >> b[i];
for(int i = 1; i <= m; i++) {
for(int j = i + 1; j <= n; j++) {
if(fabs(a[j][i]) > 1e-4 && b[j] < b[i]) {
for(int k = 1; k <= n; k++) swap(a[i][k], a[j][k]);
swap(b[i], b[j]);
}
}
if(!a[i][i]) continue;
for(int j = 1; j <= n; j++) {
if(i == j) continue;
long double rate = (a[j][i] / a[i][i]);
for(int k = i; k <= m; k++) {
a[j][k] -= a[i][k] * rate;
}
}
}
int cnt = 0, ans = 0;
for(int i = 1; i <= min(n, m); i++) {
if(fabs(a[i][i]) > 1e-4) cnt++, ans += b[i];
}
printf("%d %d\n", cnt, ans);
return 0;
}

线性基优化版

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define eps 1e-5
using namespace std;
const int MAXN = 505;
int n, m, nxt[MAXN], cnt, ans;
struct qwq{
long double num[MAXN];
int val;
bool operator < (const qwq &b) const{
return this -> val < b.val;
}
}a[MAXN];
bool f[MAXN];
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i].num[j];
}
}
for(int i = 1; i <= n; i++) cin >> a[i].val;
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(fabs(a[i].num[j]) > eps){
if(!f[j]) {
f[j] = 1;
cnt++;
ans += a[i].val;
nxt[j] = i;
break;
}else{
long double rate = a[i].num[j] / a[nxt[j]].num[j];
for(int k = j; k <= n; k++) a[i].num[k] -= rate * a[nxt[j]].num[k];
}
}
}
}
printf("%d %d\n", cnt, ans);
return 0;
}

最新文章

  1. linux查看本机IP、gateway、DNS
  2. 一道Apple公司(中国)的面试题目
  3. Gps与地图坐标转换
  4. Elasticsearch 教程--入门
  5. 关于if(a&lt;b&lt;c)判断的问题
  6. Java控制语句——break和continue
  7. c++ 设计模式3 (重构技法 Template Method)
  8. Extjs4中tabPanel
  9. vi 快捷键【转】【weber整理必出精品】
  10. errcode 4103 invalid page hint 小程序模板消息推送遇到的坑
  11. 常用的 css reset,基本的base.css
  12. Acperience HDU - 5734
  13. 一些值得深入学习和借鉴的 .Net 开源项目
  14. echarts 滚动条 缩放
  15. cannal&amp;otter源码解析
  16. spring boot metrics信息推送开发
  17. 树pao(雾)
  18. pigz 压缩
  19. 动态规划(Dynamic Programming)
  20. 【bzoj2555】 SubString

热门文章

  1. sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL [SQL: u&#39;ALTER TABLE address_scopes ADD COLUMN ip_version INTEGER NOT NULL&#39;]
  2. spring (由Rod Johnson创建的一个开源框架)
  3. core 下使用 autofac
  4. 快学UiAutomator配置编辑环境
  5. C#导入有道词典单词本到扇贝
  6. Django-C003-视图
  7. SSI的实例(登录增删改查)
  8. UIControlEvent
  9. Vuex基本概念
  10. Java语言的特点和特性