Files
test/puzzles/google_code_jam/2014/2-A.cpp

174 lines
3.5 KiB
C++

/*
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
*/
#include <assert.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <exception>
#include <stdexcept>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <string>
#include <memory>
#include <functional>
#include <algorithm>
#include <utility>
#include <limits>
#include <math.h>
typedef long long ll;
typedef std::vector<ll> vec;
void check( bool b ) { if ( !b ) std::cerr << "error" << std::endl; }
#define FOR(i,l) for ( ll i =0, ie = ll(l); i<ie; ++i )
/*
Read n values into v
*/
template <class V>
void readv( V& v, int n )
{
v.reserve(n);
for ( int i = 0; i < n; ++i )
{
typename V::value_type e;
std::cin >> e;
check( !std::cin.fail() );
v.push_back(e);
}
}
/*
Problem
Adam, being a well-organized man, has always been keenly interested in
organizing all his stuff. In particular, he fondly remembers the many hours
of his youth that were spent moving files from his computer onto Compact Discs.
There were two very important rules involved in this procedure. First, in order
to ensure that all discs could be labeled clearly, Adam would never place more
than two files on the same disc. Second, he would never divide a single file
over multiple discs. Happily, the discs he was using were always large enough
to make this possible.
Thinking back, Adam is now wondering whether he arranged his files in the best
way, or whether he ended up wasting some Compact Discs. He will provide you with
the capacity of the discs he used (all his discs had the same capacity) as well
as a list of the sizes of the files that he stored. Please help Adam out by
determining the minimum number of discs needed to store all his files—following
the two very important rules, of course.
Input
The first line of the input gives the number of test cases, T. T test cases
follow. Each test case begins with a line containing two integers: the number
of files to be stored N, and the capacity of the discs to be used X (in MBs).
The next line contains the N integers representing the sizes of the files Si
(in MBs), separated by single spaces.
Output
For each test case, output one line containing "Case #x: y", where x is the case
number (starting from 1) and y is the minimum number of discs needed to store
the given files.
Limits
1 ≤ T ≤ 100.
1 ≤ X ≤ 700.
1 ≤ Si ≤ X.
Small dataset
1 ≤ N ≤ 10.
Large dataset
1 ≤ N ≤ 104
Sample
Input
3
3 100
10 20 70
4 100
30 40 60 70
5 100
10 20 30 40 60
Output
Case #1: 2
Case #2: 2
Case #3: 3
*/
int solve_puzzle()
{
ll n, c;
std::cin >> n >> c;
vec v;
readv(v,n);
std::sort( v.begin(), v.end(), std::greater<ll>() );
int max_count = 0;
for ( int i = 0; i < v.size(); ++i )
{
if ( v[i] <= 0 )
continue;
max_count++;
ll r = c - v[i];
for ( int j = i+1; j < v.size(); ++j )
{
if ( v[j] > 0 && v[j] <= r )
{
v[j] = -1;
break;
}
}
}
return max_count;
}
int main ( void )
{try{
srand((unsigned)time(NULL));
int puzzle_count;
std::cin >> puzzle_count;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
for ( int i = 1; i <= puzzle_count; i++ )
{
std::cout << "Case #" << i << ": ";
auto r = solve_puzzle();
std::cout << r << std::endl;
}
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}