为什么有2个错呢?有赏
查看原帖
为什么有2个错呢?有赏
728949
lucas_777楼主2024/1/20 13:03
#include <bits/stdc++.h>
using namespace std;

struct Wupin {
	long long sl, price, cupcity, tot;
	string name;
};

int main() {
	long long m, n, k = 0, t, total = 0;
	cin >> m >> n;
	m = 21 - m;
	Wupin wp1[n], wp2[10*n];
	// 1. 输入
	for(int i = 0; i < n; ++i) {
		cin >> wp1[i].sl >> wp1[i].price >> wp1[i].cupcity >> wp1[i].name;
	}
	// 2. 合并
	for(int i = 0; i < n; ++i) {
		if(wp1[i].name != "") {
			for(int j = i+1; j < n; ++j) {
				if(wp1[i].name == wp1[j].name) {
					wp1[i].sl += wp1[j].sl;
					wp1[j].name = "";
				}
			}
			// 合并后,按容量分解到 wp2, 并计算价值
			while(wp1[i].sl > 0) {
				if(wp1[i].sl >= wp1[i].cupcity) {
					t = wp1[i].cupcity;
				} else {
					t = wp1[i].sl;
				}
				wp1[i].sl -= wp1[i].cupcity;
				wp2[k++] = {t, wp1[i].price, wp1[i].cupcity, wp1[i].price*t, wp1[i].name};
			}
		}
	}
	sort(wp2, wp2+k, [&](Wupin w1, Wupin w2) -> bool { return w1.tot > w2.tot; });
	for(int i = 0; i < m; ++i) {
		total += wp2[i].tot;
	}
	cout << total << endl;
	return 0;
}
2024/1/20 13:03
加载中...