Write a program below |
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
struct sand {
int cost, weight;
double cw;
sand() { }
sand(int _cost, int _weight) {
this->cost = _cost;
this->weight = _weight;
this->cw = 1. * _cost / _weight;
}
};
bool cmp(sand a, sand b) {
|
|
}
vector<sand>sandArray(0);
int n;
int w;
double answer;
int main() {
cin >> n >> w;
sandArray.resize(n);
for (int i = 0; i < n; i++) {
int cost, weight;
cin >> cost >> weight;
sandArray.at(i) = sand(cost, weight);
}
sort(sandArray.begin(), sandArray.end(), cmp);
for (int i = 0; i < n; i++)
if (sandArray.at(i).weight <= w) {
w -= sandArray.at(i).weight;
answer += sandArray.at(i).cost;
}
else {
answer += sandArray.at(i).cw * w;
w = 0;
}
printf("%.4lf", answer);
}
|