master
Yorick van Pelt 2016-09-24 21:16:39 +02:00
commit f9a2e40404
14 changed files with 531 additions and 0 deletions

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
FILES := bapc-d bapc-a bapc-e bapc-j bapc-i bapc-f bapc-b
all: $(FILES)
clean:
rm -f $(FILES)
%: %.cpp
g++ -static -g -O2 -std=gnu++0x $< -o $@

7
README.md Normal file
View File

@ -0,0 +1,7 @@
These are our solutions for the [BAPC](http://2015.bapc.eu/) preliminaries 2015.
See also the [problem set](http://2015.bapc.eu/problems/bapc-preliminaries-2015.tar.gz)
Authors:
- Sal Wolffs
- Lars Jellema
- Yorick van Pelt

33
bapc-a.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <stack>
#include <cmath>
bool is_square(long long x) {
return (round(sqrt(x)) * round(sqrt(x))) == x;
}
int main(int argc, char const *argv[])
{
int T;
std::cin >> T;
for (int i = 0; i < T; ++i)
{
long long X, Y;
long long D;
double Df;
std::cin >> D;
std::cin >> X;
std::cin >> Y;
if (!((X * X + Y * Y) % D) && is_square((X*X + Y*Y) / D)) {
std::cout << (long long)round((X*X + Y*Y) / D) << std::endl;
continue;
}
Df = sqrt(D);
long double ans = sqrt(((X * X) + (Y * Y))) / Df;
if ((long long)ceil(ans) == 1) ans = 2;
//if (ans != 0) ans -= 1e-6;
std::cout << (long long)ceil(ans) << std::endl;
}
return 0;
}

23
bapc-b-sample Normal file
View File

@ -0,0 +1,23 @@
1
3 21
1 2
1 3
1 3
1 4
1 5
1 6
2 3
2 3
2 4
2 5
2 6
2 6
3 4
3 5
3 5
3 6
4 5
4 5
4 6
5 6
5 6

6
bapc-b-sample0 Normal file
View File

@ -0,0 +1,6 @@
1
2 4
1 3
2 4
3 4
1 2

65
bapc-b.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include <cstring>
#include <list>
#include <vector>
typedef std::vector< std::vector<bool> > Graph;
int recurse(Graph &graph, bool* todo, int j, int N, std::vector<int> &members) {
if (!todo[j]) return 0;
todo[j] = false;
members.push_back(j);
int total = 1;
for (int k = 0; k < N; k++) {
if (graph[j][k]) {
total += recurse(graph, todo, k, N, members);
}
}
return total;
}
int main(int argc, char const *argv[])
{
int C;
std::cin >> C;
for (int i = 0; i < C; ++i)
{
int N, M;
std::cin >> N;
std::cin >> M;
N *= 2;
Graph graph(N, std::vector<bool>(N));
for (int j = 0; j < N; j++) {
for(int k = 0; k < N; k++) {
graph[j][k] = (j != k);
}
}
for (int j = 0; j < M; j++) {
int A, B;
std::cin >> A;
std::cin >> B;
A--; B--;
if (A == N || B == N) {
std::cerr << "too high" << std::endl;
}
graph[A][B] = false;
graph[B][A] = false;
}
std::list<int> groups;
std::list< std::vector<int> > group_members;
int temp = N;
bool todo[N];
for(int j = 0; j < N; j++) {
if (!todo[j]) continue;
group_members.emplace_back(std::vector<int>());
groups.push_back(recurse(graph, todo, j, N, group_members.back()));
}
for (int c : groups) {
std::cout << "group: " << c << std::endl;
}
/* code */
}
return 0;
}

34
bapc-c.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <iostream>
#include <cstring>
int main(int argc, char const *argv[])
{
int T;
std::cin >> T;
for(int i = 0; i < T; i++) {
int C;
std::cin >> C;
char field[1000][1000];
memset(field, 1000*1000, 0);
int xpos = 500;
int ypos = 500;
for (int j = 0; j < C; j++) {
char letter;
std::cin >> letter;
int magnitude;
std::cin >> magnitude;
switch(letter) {
case 'x':
while(magnitude--) {
field[x++][y] = 1;
}
break;
case 'y':
case 'z':
}
}
}
return 0;
}

65
bapc-d.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include <stack>
#include <utility>
#include <vector>
using std::vector;
typedef vector<vector<char> > Board;
int check_white(Board &board, std::stack< std::pair<int, int> > &todo, int row, int col, int N) {
if (row >= 0 && col >= 0 && row < N && col < N) {
if (board[row][col] == '-') {
board[row][col] = 'w';
todo.push(std::make_pair(row, col));
return 1;
}
}
return 0;
}
int main(int argc, char const *argv[])
{
int T;
std::cin >> T;
for (int i = 0; i < T; ++i)
{
int N;
std::cin >> N;
Board board(N,vector<char>(N));
for (int row = 0; row < N; row++) {
char temp[N+1];
std::cin >> temp;
for(int col = 0; col < N; col++) {
board[row][col] = temp[col];
}
}
// fill stack with whites
std::stack< std::pair<int, int> > todo;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (board[row][col] == 'w') {
todo.push(std::pair<int, int>(row, col));
}
}
}
int no_made_white = 0;
while(!todo.empty()) {
std::pair<int, int> n = todo.top();
todo.pop();
int row = n.first;
int col = n.second;
no_made_white += check_white(board, todo, row-1, col-1, N);
no_made_white += check_white(board, todo, row-1, col, N);
no_made_white += check_white(board, todo, row-1, col+1, N);
no_made_white += check_white(board, todo, row, col-1, N);
no_made_white += check_white(board, todo, row, col+1, N);
no_made_white += check_white(board, todo, row+1, col-1, N);
no_made_white += check_white(board, todo, row+1, col, N);
no_made_white += check_white(board, todo, row+1, col+1, N);
}
std::cout << no_made_white << std::endl;
}
return 0;
}

36
bapc-e.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <stack>
#include <cmath>
int main(int argc, char const *argv[])
{
int T;
std::cin >> T;
for (int i = 0; i < T; ++i)
{
int N;
std::cin >> N;
int minimum = 0;
int min_delta_g = 0x7FFFFFFF;
int delta_g = 0;
for(int j = 0; j < N; j++) {
int G,D;
std::cin >> G;
std::cin >> D;
delta_g += G - D;
//std::cout << "station " << j << " delta_g: " << delta_g << " minimum:" << min_delta_g << " (id: " << minimum << ")" << std::endl;
if (delta_g < min_delta_g) {
minimum = j;
min_delta_g = delta_g;
}
}
if (delta_g < 0) {
std::cout << "IMPOSSIBLE" << std::endl;
} else {
std::cout << (((minimum + 1) % N) + 1) << std::endl;
}
}
return 0;
}

76
bapc-f.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <cstdio>
#include <iostream>
#include <set>
#include <cstring>
#include <utility>
#include <vector>
using std::string;
struct block {
int n;
int no_connected;
block* connected[4];
};
void fill(block* b, int n) {
if (b->n != -1) return;
b->n = n;
for(int i = 0; i < b->no_connected; i++) {
fill(b->connected[i], n);
}
}
int main(int argc, char const *argv[])
{
int T;
std::cin >> T;
for (int i = 0; i < T; ++i)
{
int n,m,p,c;
std::cin >> n;
std::cin >> m;
std::cin >> p;
std::cin >> c;
block blocks[n][m];
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++) {
blocks[x][y].n = -1;
blocks[x][y].no_connected = 0;
}
block* powered[p];
for(int j = 0; j < p; j++) {
int x,y;
std::cin >> x;
std::cin >> y;
powered[j] = &blocks[x][y];
}
for(int j = 0; j < c; j++) {
int x,y; char d;
std::cin >> x;
std::cin >> y;
std::cin >> d;
if (d == 'R') {
blocks[x][y].connected[blocks[x][y].no_connected++] = &blocks[x+1][y];
blocks[x+1][y].connected[blocks[x+1][y].no_connected++] = &blocks[x][y];
} else {
blocks[x][y].connected[blocks[x][y].no_connected++] = &blocks[x][y+1];
blocks[x][y+1].connected[blocks[x][y+1].no_connected++] = &blocks[x][y];
}
}
for (int j = 0; j < p; j++) {
fill(powered[j], 0);
}
int cur_groups = 0;
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++) {
if (blocks[x][y].n == -1) {
fill(&blocks[x][y], ++cur_groups);
}
}
std::cout << cur_groups << std::endl;
}
return 0;
}

77
bapc-i.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <cstdio>
#include <iostream>
#include <set>
#include <cstring>
#include <utility>
using std::string;
enum Color {B, G, R, Y};
Color colors[] = {B, G, R, Y};
typedef unsigned char Value;
typedef std::pair<Color, Value> Tile;
typedef std::set<Tile> SortCounter;
Color to_col(char c) {
switch (c) {
case 'b': return B;
case 'g': return G;
case 'r': return R;
case 'y': return Y;
default: throw;
}
}
void record(string token, SortCounter &counts) {
counts.emplace(Tile(to_col(token.back()),
std::stoul(token.substr(0, token.size()-1))));
}
bool rummisearch(SortCounter &counts) {
for (Color c : colors) {
for (Value v = 1; v <= 98; ++v) {
if (counts.count(Tile(c, v))
&& counts.count(Tile(c, v+1))
&& counts.count(Tile(c, v+2))) {
return true;
}
}
}
for (Value v = 1; v <= 100; ++v) {
int i = 0;
for (Color c : colors) {
if (counts.count(Tile(c, v))) {
++i;
}
if (i >= 3) return true;
}
}
return false;
}
int main(int argc, char const *argv[])
{
int T;
std::cin >> T;
for (int i = 0; i < T; ++i)
{
int M;
std::cin >> M;
SortCounter counter;
for (int j = 0; j < M; j++) {
std::string in;
std::cin >> in;
record(in, counter);
}
if (rummisearch(counter)) {
std::cout << "YES" << std::endl;
} else {
std::cout << "NO" << std::endl;
}
}
return 0;
}

73
bapc-j.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <cstdio>
#include <iostream>
#include <cstring>
bool is_impossible(int letters[], int no_chars, int leftoff) {
for (int j = 0; j < 26; j++) {
if (letters[j] > ((no_chars+1) / 2)) {
return true;
}
}
return false;
}
int main(int argc, char const *argv[])
{
int T;
std::cin >> T;
for (int i = 0; i < T; ++i)
{
int letters[26];
for(int j = 0; j < 26; j++) {
letters[j] = 0;
}
// inlezen
char c = getchar();
int no_chars = 0;
// count
if (c == '\n') c = getchar();
while(c != '\n') {
letters[c - 'a']++;
no_chars++;
c = getchar();
}
if (is_impossible(letters, no_chars, -1)) {
printf("IMPOSSIBLE\n");
continue;
}
char last = -1;
while(no_chars > 0) {
bool printed = false;
for (int j = 0; j < 26; j++) {
if (letters[j] > ((no_chars) / 2)) {
last = j;
letters[j]--;
putchar(j+'a');
printed = true;
break;
}
}
if (printed) {
no_chars--;
continue;
}
for(int j = 0; j < 26; j++) {
if (letters[j] && last != j) {
last = j;
letters[j]--;
putchar(j+'a');
break;
}
}
//printf("no more letters?!\n");
//return -1;
no_chars--;
}
putchar('\n');
}
return 0;
}

13
test-x.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
int main(int argc, char const *argv[])
{
unsigned int t;
std::cin >> t;
for (int i = 0; i < t; i++) {
unsigned int v;
std::cin >> v;
std::cout << (v + 1) << std::endl;
}
return 0;
}

16
test-y.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
#include <iomanip>
#include <cmath>
int main(int argc, char const *argv[])
{
unsigned int t;
std::cin >> t;
for (int i = 0; i < t; i++) {
double a;
std::cin >> a;
std::cout << std::setprecision(9);
std::cout << sqrt(a / M_PI)*2.0 << std::endl;
}
return 0;
}