Skip to content

Instantly share code, notes, and snippets.

@na-o-ys
Last active August 29, 2015 14:13
Show Gist options
  • Save na-o-ys/893133b3d0a49f56d6c9 to your computer and use it in GitHub Desktop.
Save na-o-ys/893133b3d0a49f56d6c9 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main_calc(int n, int m, stack<int> **tray);
int main(int argc, char const* argv[])
{
int n, m;
int size;
int val;
stack<int> *tray[3];
while (true) {
cin >> n >> m;
if (n == 0 && m == 0) break;
for (int i = 0; i < 3; i++) {
cin >> size;
tray[i] = new stack<int>;
for (int j = 0; j < size; j++) {
cin >> val;
tray[i]->push(val);
}
}
cout << main_calc(n, m, tray) << endl;
}
cin >> n >> m;
return 0;
}
enum Move {
AB, BA, BC, CB, NO
};
void move(stack<int> **tray, Move move) {
int tray_0 = tray[0]->size() == 0 ? 0 : tray[0]->top();
int tray_1 = tray[1]->size() == 0 ? 0 : tray[1]->top();
int tray_2 = tray[2]->size() == 0 ? 0 : tray[2]->top();
switch (move) {
case AB:
tray[1]->push(tray_0);
if (tray[0]->size() > 0) tray[0]->pop();
break;
case BA:
tray[0]->push(tray_1);
if (tray[1]->size() > 0) tray[1]->pop();
break;
case BC:
tray[2]->push(tray_1);
if (tray[1]->size() > 0) tray[1]->pop();
break;
case CB:
tray[1]->push(tray_2);
if (tray[2]->size() > 0) tray[2]->pop();
break;
case NO:
break;
}
}
vector<Move>* get_movables(stack<int> **tray, Move lastmove) {
auto moves = new vector<Move>;
int tray_0 = tray[0]->size() == 0 ? 0 : tray[0]->top();
int tray_1 = tray[1]->size() == 0 ? 0 : tray[1]->top();
int tray_2 = tray[2]->size() == 0 ? 0 : tray[2]->top();
// AB
if (tray_0 > tray_1 && lastmove != BA) {
moves->push_back(AB);
}
// BA
if (tray_1 > tray_0 && lastmove != AB) {
moves->push_back(BA);
}
// BC
if (tray_1 > tray_2 && lastmove != CB) {
moves->push_back(BC);
}
// CB
if (tray_2 > tray_1 && lastmove != BC) {
moves->push_back(CB);
}
return moves;
}
stack<int>** copy(stack<int> **tray) {
auto **new_tray = new stack<int>*[3];
new_tray[0] = new stack<int>(*tray[0]);
new_tray[1] = new stack<int>(*tray[1]);
new_tray[2] = new stack<int>(*tray[2]);
return new_tray;
}
int single_calc(int m, stack<int> **tray, Move first_move = NO) {
int count = 0;
auto *moves = get_movables(tray, first_move);
while (true) {
int tray_0 = tray[0]->size() == 0 ? 0 : tray[0]->top();
int tray_1 = tray[1]->size() == 0 ? 0 : tray[1]->top();
int tray_2 = tray[2]->size() == 0 ? 0 : tray[2]->top();
// cout << tray_0 << " " << tray_1 << " " << tray_2 << endl;
// cout << count << ": " << moves->size() << endl;
if (tray[1]->empty() && (tray[0]->empty() || tray[1]->empty())) break;
if (count == m || moves->size() == 0) {
return -1;
}
move(tray, (*moves)[0]);
moves = get_movables(tray, (*moves)[0]);
count++;
}
return count;
}
int main_calc(int n, int m, stack<int> **tray) {
if (tray[1]->empty() && (tray[0]->empty() || tray[1]->empty())) return 0;
int count = 0, min = m;
auto *moves = get_movables(tray, NO);
if (moves->size() == 0) return -1;
for (auto& move_type : *moves) {
auto copy_tray = copy(tray);
move(copy_tray, move_type);
count = single_calc(m - 1, copy_tray, move_type);
if (count != -1 && count + 1 < min) min = count + 1;
}
return min;
}
#include <bits/stdc++.h>
#define loop(n, i) for(int i=0;i<n;i++)
using namespace std;
int calc(int a, int b, int c)
{
if (!a && !b) return 0;
if (a&1) return calc(a>>1, b>>1, c>>1) + 2 + 2 * calc((a|b|c)>>1, 0, 0);
if (b&1) return calc(c>>1, b>>1, a>>1) + 1 + calc((a|b|c)>>1, 0, 0);
return calc(a>>1, b>>1, c>>1);
}
int main()
{
while (1) {
int n, m; cin >> n >> m;
int cup[3] = {};
loop (3, i) {
int c; cin >> c;
while (c--) {
int v; cin >> v;
cup[i] |= 1 << v--;
}
}
int ans = min(calc(cup[0], cup[1], cup[2]), calc(cup[2], cup[1], cup[0]));
cout << (ans <= m ? ans : -1) << endl;
}
return 0;
}
#include <bits/stdc++.h>
#define loop(n, i) for(int i=0;i<n;i++)
using namespace std;
int calc(vector<int> cup, int m)
{
int cnt = 0;
while (1) {
int i = cnt%2, j = i+1;
int p = 1;
while (p <= max(cup[i], cup[j])) p <<= 1;
p >>= 1;
if (!p) break;
cup[i] ^= p;
cup[j] ^= p;
cnt++;
if (cnt > m) return cnt;
}
return cnt;
}
int main()
{
while (1) {
int n, m; cin >> n >> m;
if (n == 0 && m == 0) break;
vector<int> cup(3);
loop (3, i) {
int c; cin >> c;
while (c--) {
int v; cin >> v;
cup[i] |= 1 << v--;
}
}
int ans = calc(cup, m);
swap(cup[0], cup[2]);
ans = min(ans, calc(cup, m));
cout << (ans <= m ? ans : -1) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment