3AC 2WA
查看原帖
3AC 2WA
269233
shzps楼主2024/7/30 15:34
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;

struct Block {
	int matt;
	int money;
	void readme() {
		cin >> matt >> money;
	}
};

//输入
int readthem(vector<Block> &b, int &N) {
	int T;
	cin >> N >> T;
	b.resize(N);
	for (long long unsigned i = 0; i < b.size(); i++) {
		b[i].readme();
	}
	return T;
}

//直接使用性价比进行比较
bool Comp(Block x, Block y) {
	return ((double)x.money / x.matt) > ((double)y.money / y.matt);
}

void solve(vector<Block> b, int &T, double &ans) {
	sort(b.begin(), b.end(), Comp);
	int W1 = 0;
	ans = 0;
	for (int i = 0; i < b.size() || W1 < T; i++) {
		if (W1 < T && W1 + b[i].matt <= T) {
			ans += b[i].money;
			W1 += b[i].matt;
			if (W1 == T)
				return;
		}
		if (T >= W1 && W1 + b[i].matt > T) {
			ans += (T - W1) * ((double)b[i].money / b[i].matt);
			return;
		}
	}

}

void display(double ans) {
	printf("%.2lf", ans);
}

int main() {//主函数
	vector<Block> a;
	int  N;
	int A = readthem(a, N);//输入
	double ans;
	solve(a, A, ans);
	display(ans);
	return 0;
}
2024/7/30 15:34
加载中...