Problem

10/11

모래

Problem

프로그램을 수정하여 다음 문제를 해결하십시오.

상점을 털다가 도둑이 금가루 N 상자를 발견했습니다. i라는 상자에서 모래는 vi의 값과 wi의 무게를 가집니다. >. 전리품을 옮기기 위해 도둑은 배낭을 사용합니다. 배낭의 운반 용량이 W로 제한되는 경우 강도가 운반할 수 있는 최대 모래 비용을 결정해야 합니다.
 
상자에서 원하는 양의 모래를 부을 수 있습니다. 그러면 부어진 모래 비용과 전체 상자 비용의 비율은 부어진 모래 부피와 전체 상자 부피의 비율과 같습니다.
 
입력
입력 파일의 첫 번째 줄에는 두 개의 숫자가 포함되어 있습니다.  - NW(1 <= N <= 1000, 0 <= W <= 1000000). 그 다음에는 각각 두 개의 정수로 된 N 줄이 나옵니다. i번째 줄에는 비용 vi와 가중치 wi가 포함됩니다. i번째 서랍에 있는 모래. 모든 숫자는 음수가 아니며 106을 초과하지 않습니다.
 
출력
0.0001 이하의 오류로 원하는 최대 비용을 인쇄하십시오.

 
<헤드> <몸>
# 입력 출력
1
3 50
60 20
100 50
120 30
180.0000
Write the 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);
}    

     

Program check result

To check the solution of the problem, you need to register or log in!