Move Google Code Jam under puzzles.

This commit is contained in:
2015-02-08 23:12:41 +04:00
parent e2841a251e
commit 7a53e5a3a1
64 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,231 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM: let g:cppflags=g:Iboost.g:Itbb
VIM: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc.g:tbbmproxy
VIM: let g:ldlibpath=g:Bboost.g:Btbb
VIM: let g:argv=""
*/
/*
Problem A. Charging Chaos
This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
8 points
Judge's response for last submission: Correct.
Large input
17 points
Judge's response for last submission: Correct.
Problem
Shota the farmer has a problem. He has just moved into his newly built farmhouse, but it turns out that the outlets haven't been configured correctly for all of his devices. Being a modern farmer, Shota owns a large number of smartphones and laptops, and even owns a tablet for his favorite cow Wagyu to use. In total, he owns N different devices.
As these devices have different specifications and are made by a variety of companies, they each require a different electric flow to charge. Similarly, each outlet in the house outputs a specific electric flow. An electric flow can be represented by a string of 0s and 1s of length L.
Shota would like to be able to charge all N of his devices at the same time. Coincidentally, there are exactly N outlets in his new house. In order to configure the electric flow from the outlets, there is a master control panel with L switches. The ith switch flips the ith bit of the electric flow from each outlet in the house. For example, if the electric flow from the outlets is:
Outlet 0: 10
Outlet 1: 01
Outlet 2: 11
Then flipping the second switch will reconfigure the electric flow to:
Outlet 0: 11
Outlet 1: 00
Outlet 2: 10
If Shota has a smartphone that needs flow "11" to charge, a tablet that needs flow "10" to charge, and a laptop that needs flow "00" to charge, then flipping the second switch will make him very happy!
Misaki has been hired by Shota to help him solve this problem. She has measured the electric flows from the outlets in the house, and noticed that they are all different. Decide if it is possible for Shota to charge all of his devices at the same time, and if it is possible, figure out the minimum number of switches that needs to be flipped, because the switches are big and heavy and Misaki doesn't want to flip more than what she needs to.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of three lines. The first line contains two space-separated integers N and L. The second line contains N space-separated strings of length L, representing the initial electric flow from the outlets. The third line also contains N space-separated strings of length L, representing the electric flow required by Shota's devices.
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 switches to be flipped in order for Shota to charge all his devices. If it is impossible, y should be the string "NOT POSSIBLE" (without the quotes). Please note that our judge is not case-sensitive, but it is strict in other ways: so although "not possible" will be judged correct, any misspelling will be judged wrong. We suggest coping/pasting the string NOT POSSIBLE into your code.
Limits
1 ≤ T ≤ 100.
No two outlets will be producing the same electric flow, initially.
No two devices will require the same electric flow.
Small dataset
1 ≤ N ≤ 10.
2 ≤ L ≤ 10.
Large dataset
1 ≤ N ≤ 150.
10 ≤ L ≤ 40.
Sample
Input
Output
3
3 2
01 11 10
11 00 10
2 3
101 111
010 001
2 2
01 10
10 01
Case #1: 1
Case #2: NOT POSSIBLE
Case #3: 0
Explanation
In the first example case, Misaki can flip the second switch once. The electric flow from the outlets becomes:
Outlet 0: 00
Outlet 1: 10
Outlet 2: 11
Then Shota can use the outlet 0 to charge device 1, the outlet 1 to charge device 2, outlet 2 to charge device 0. This is also a solution that requires the minimum amount number of switches to be flipped.
*/
#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>
using namespace std;
typedef long long i64;
i64 cmp( std::vector<i64>& out, std::vector<i64>& dev, i64 m )
{
std::sort( out.begin(), out.end() );
for ( i64 j = 0; j < out.size(); ++j)
{
if ( (out[j]&m) != (dev[j]&m) )
return false;
}
return true;
}
i64 rec( std::vector<i64>& out, std::vector<i64>& dev, i64 m, i64 l )
{
l>>=1;
if ( !l )
return 0;
m|=l;
i64 p = -1;
if ( cmp(out,dev,m) )
p = rec( out, dev, m, l);
auto cpy = out;
i64 n = (i64)out.size();
for ( i64 j = 0; j < n; ++j)
cpy[j] ^= l;
i64 s = -1;
if ( cmp(cpy,dev,m) )
s = rec( cpy, dev, m, l);
if ( s >= 0 )
s++;
if ( p >= 0 && s >= 0)
return std::min(p,s);
else
return std::max(p,s);
}
i64 solve_puzzle()
{
i64 n, l;
cin >> n >> l;
std::vector<i64> out;
std::vector<i64> dev;
for ( i64 i = 0; i < 2*n; ++i )
{
std::string s;
cin >> s;
i64 c = 0;
for ( i64 j = 0; j < l; ++j )
c = c << 1 | (s[j]=='1');
if ( i < n )
out.push_back(c);
else
dev.push_back(c);
}
std::sort( dev.begin(), dev.end() );
i64 s = rec(out,dev,0,i64(1)<<l);
for ( i64 i = i64(1)<<(l-1); i; i >>=1)
{
i64 o = 0;
for ( auto j : out )
o += (j&i) ? 1 : 0;
i64 d = 0;
for ( auto j : dev )
d += (j&i)? 1 : 0;
if ( d != o && (n-d)!=o && s >= 0 )
cout << "error" << std::endl;
if ( d != o && (n-d)!=o && s < 0 )
{
// cout << "interesting " ;
// break;
}
}
return s;
}
int main ( void )
{try{
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();
if ( r < 0 )
std::cout << "NOT POSSIBLE" << std::endl;
else
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;
}}