Skip to content

Instantly share code, notes, and snippets.

@benblack769
Created September 19, 2016 01:38
Show Gist options
  • Save benblack769/3249ebeea3f42a725fe7ee03638d1e4e to your computer and use it in GitHub Desktop.
Save benblack769/3249ebeea3f42a725fe7ee03638d1e4e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <cmath>
#include <set>
//#include <unordered_set>
//#define set unordered_set
using namespace std;
bool works(int cursum,int i,int min,int max,vector<int> & nums,int restsum){
if (cursum >= min && cursum <= max)
return true;
if(cursum > max ||
cursum + restsum < min ||
0 > i)
return false;
int newsum = cursum + nums[i];
int nrestsum = restsum - nums[i];
if(works(cursum,i-1,min,max,nums,nrestsum) ||
works(newsum,i-1,min,max,nums,nrestsum))
return true;
else
return false;
}
void do_swallow(string line){
istringstream iss(line);
vector<string> vec{istream_iterator<string>(iss),istream_iterator<string>()};
int n_insects = stoi(vec[2]);
vector<int> weights(n_insects);
int cmin = stoi(vec[0]);
int cmax = stoi(vec[1]);
int sum = 0;
for(int i = 0; i < n_insects;i++){
int num = stoi(vec[i+3]);
weights[i] = num;
sum += num;
}
if(works(0,weights.size()-1,cmin,cmax,weights,sum))
cout << "Sallow swallow swallows.";
else
cout << "Sallow swallow wallows in dust.";
}
int main(){
string str;
getline(cin,str);
int n_swallows = stoi(str);
for(int i = 0; i < n_swallows; i++){
getline(cin,str);
do_swallow(str);
if (i != n_swallows-1)
cout << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment