개발일지/Algorithm
20.07.15 개발일지 - 정육점(fail)
RedChiken
2020. 7. 15. 22:09
#include <iostream>
#include <vector>
#include <algorithm>
struct chunk {
unsigned int weight;
unsigned int price;
};
using namespace std;
int main()
{
unsigned int length = 0, needs = 0, weight = 0, price = 0, before = -1;
vector<struct chunk> meatlist;
cin >> length >> needs;
for (int i = 0; i < length; ++i)
{
cin >> weight >> price;
meatlist.push_back(chunk{ weight, price });
}
sort(meatlist.begin(), meatlist.end(), [](const auto& a, const auto& b) { return ((a.price < b.price) || (a.price == b.price) && (a.weight > b.weight)); });
weight = price = 0;
for (const auto& iter : meatlist)
{
if (weight < needs)
{
if (price > iter.price)
{
before = price = iter.price;
}
else
{
if (before == iter.price)
{
price += iter.price;
}
else
{
before = price = iter.price;
}
}
}
else
{
if ((price > iter.price) && (before != iter.price))
{
before = price = iter.price;
}
}
weight += iter.weight;
}
if (weight < needs)
{
cout << -1 << endl;
}
else
{
cout << price << endl;
}
return 0;
}
구상한 코드가 틀려 몇일 정도 다시 예외 상황이나 문제점을 고민해보도록 하겠습니다.