- 알고리즘이 왜 빨리빨리 안 떠오르는 걸까
- 요것도 1시간 넘게 걸렸다--; 선호랑 연습을 해야 하는걸까
~cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
bool IsDividable(vector<int>& weights, int diff);
int GetMax(vector<int>& weights, int diff);
int GetMin(vector<int>& weights, int diff);
int main()
{
int weight, diff, num;
vector<int> weights;
cin >> num;
cin >> diff;
for(int i = 0 ; i < num ; ++i)
{
cin >> weight;
weights.push_back(weight);
}
if(IsDividable(weights, diff))
cout << "Yes";
else
cout << "No";
return 0;
}
bool IsDividable(vector<int>& weights, int diff)
{
sort(weights.begin(), weights.end());
int partTotal = 0;
for(int i = 0 ; i < weights.size() ; ++i)
{
partTotal = weights[i];
for(int j = i + 1 ; j < weights.size() ; ++j)
{
if(i != j)
{
partTotal += weights[j];
if(partTotal < GetMin(weights, diff))
continue;
else if(partTotal >= GetMin(weights, diff) && partTotal <= GetMax(weights, diff))
return true;
else
{
partTotal -= (weights[j] + weights[j-1]);
continue;
}
partTotal = 0;
}
}
}
return false;
}
int GetMax(vector<int>& weights, int diff)
{
float total = accumulate(weights.begin(), weights.end(), 0) / 2;
return (total + diff/2);
}
int GetMin(vector<int>& weights, int diff)
{
float total = accumulate(weights.begin(), weights.end(), 0) / 2;
return (total - diff/2);
}