[KOI]전국본선 2013 중등부 두번째 문제




#include <stdio.h>

int main()
{
	int n;
	int after[30] = { 0, }, before[30] = { 0, };
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &after[i]);
	}
	int cur = 0, count = 0;
	while (count < n) {
		while (before[cur] != 0) {
			cur = (cur + 1) % n;
		}
		before[cur] = after[count++];
		cur = (before[cur] + cur) % n;
	}

	printf("%d\n", n);
	for (int i = 0; i < n; ++i) {
		printf("%d ", before[i]);
	}

	return 0;
}


설정

트랙백

댓글

[KOI]전국본선 2013 중등부 첫번째 문제





#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

struct ANIMAL {
	int y, x;
};

vector<int> hunter;
vector<animal> animal;

bool comp(const ANIMAL& a, const ANIMAL& b) {
	return (a.x < b.x);
}

bool is_kill(const ANIMAL& a, const int h, const int len) {
	int dist = abs(a.x - h) + a.y;
	if (dist <= len)		return true;
	else					return false;
}

int main()
{
	int M, N, L;
	scanf("%d %d %d", &M, &N, &L);

	hunter.resize(M);
	animal.resize(N);

	for (int i = 0; i < M; ++i) {
		scanf("%d", &hunter[i]);
	}

	for (int i = 0; i < N; ++i) {
		scanf("%d %d", &animal[i].x, &animal[i].y);
	}

	sort(hunter.begin(), hunter.end());
	sort(animal.begin(), animal.end(), comp);

	int ret = 0;
	for (auto ani : animal) {
		auto target = lower_bound(hunter.begin(), hunter.end(), ani.x) - hunter.begin();
		if (is_kill(ani, hunter[target], L)) {
			++ret;
		}
		else {
			if (target > 0) {
				--target;
				if (is_kill(ani, hunter[target], L)) {
					++ret;
				}
			}
		}
	}

	printf("%d\n", ret);
	
	return 0;
}


설정

트랙백

댓글

[KOI]전국본선 2012 중등부 첫번째 문제



#include <stdio.h>
#include <algorithm>
using namespace std;

int n, money;
int city[10000];

bool valid(int mid) {
	int ret = 0;
	for (int i = 0; i < n; ++i) {
		ret += min(mid, city[i]);
	}
	return (ret <= money);
}

int main()
{
	int left = 0, right = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &city[i]);
		right = max(right, city[i]);
	}
	scanf("%d", &money);

	while (left != right) {
		int mid = (left + right + 1) / 2;
		if (valid(mid)) {
			left = mid;
		}
		else {
			right = mid - 1;
		}
	}

	printf("%d\n", left);

	return 0;
}


설정

트랙백

댓글

Hash

Data Structure 2019. 1. 22. 22:59



#include <stdio.h>
#include <map>
#include <algorithm>
#include "Windows.h"
using namespace std;

const int PN = 23;
const int HASH_SIZE = 10000;

int table[HASH_SIZE][50];
int hash_size = 0;
char hash_raw[30000][100];

char input[30000][100];
map<char*, int> test;

unsigned int get_key(char str[]) {
	unsigned int key = 0, p = 1;

	for (int i = 0; str[i] != 0; ++i) {
		key += (str[i] * p);
		p *= PN;
	}

	return (key % HASH_SIZE);
}

int my_strcamp(char a[], char b[]) {
	int i = 0, j = 0;
	while (a[i]) {
		if (a[i++] != b[j++]) {
			--i, --j;
			break;
		}
	}
	return (a[i] - b[j]);
}

int contain(char str[]) {
	unsigned int key = get_key(str);
	int size = table[key][0];
	for (int i = 1; i <= size; ++i) {
		int pos = table[key][i];
		if (my_strcamp(hash_raw[pos], str) == 0) {
			return pos;
		}
	}
	return -1;
}

void add(char str[]) {
	int len = 0;
	for (len = 0; str[len] != 0; ++len) {
		hash_raw[hash_size][len] = str[len];
	}
	hash_raw[hash_size][len] = 0;

	unsigned int key = get_key(str);
	int& size = table[key][0];
	table[key][++size] = hash_size;

	++hash_size;
}

bool remove(char str[]) {
	unsigned int key = get_key(str);
	int& size = table[key][0];
	for (int i = 1; i <= size; ++i) {
		int pos = table[key][i];
		if (my_strcamp(hash_raw[pos], str) == 0) {
			for (int j = i + 1; j <= size; ++j) {
				table[key][j - 1] = table[key][j];
			}
			--size;
			return true;
		}
	}
	return false;
}

int make_int(int min, int max) {
	return (rand() % (max - min)) + min;
}

char make_char() {
	int val = rand() % 52;
	if (val < 26) {
		return static_cast<char>('a' + val);
	}
	return static_cast<char>('A' + val - 26);
}

int main()
{
	for (int i = 0; i < 30000; ++i) {
		int len = make_int(10, 100);
		for (int j = 0; j < len; ++j) {
			input[i][j] = make_char();
		}
		input[i][len] = 0;

		if (contain(input[i]) == -1) {
			add(input[i]);
		}
		test[input[i]] = i;


		if (i > 20000) {
			int cmd = make_int(0, 5);
			int index = make_int(0, i);
			if (cmd == 0) {
				if (contain(input[index]) != -1) {
					remove(input[index]);
				}

				test.erase(input[index]);
			}
			if (cmd == 1) {
				int my_pos = contain(input[index]);
				map<char*, int>::iterator iter = test.find(input[index]);
				int stl_pos = -1;
				if (iter != test.end()) {
					stl_pos = iter->second;
				}

				if (my_pos != stl_pos) {
					printf("find error!!!\n");
				}
			}
		}
	}

	int my_hash_size = 0;
	for (int i = 0; i < HASH_SIZE; ++i) {
		my_hash_size += table[i][0];
	}

	if (my_hash_size != test.size()) {
		printf("remove error!!!\n");
	}

	return 0;
}


설정

트랙백

댓글

Heap

Data Structure 2019. 1. 11. 22:36





#include <stdio.h>
#include <algorithm>
#include "Windows.h"
using namespace std;

int heap_size;
int heap[10000];

void push(int data) {
	int target = heap_size + 1;
	while (target != 1 && heap[target / 2] < data) {
		heap[target] = heap[target / 2];
		target /= 2;
	}
	heap[target] = data;
	++heap_size;
}

void pop() {
	int parent = 1, child = 2;
	int temp = heap[heap_size];
	while (child < heap_size) {
		if (child + 1 < heap_size && heap[child] < heap[child + 1]) {
			++child;
		}
		if (temp >= heap[child]) {
			break;
		}
		heap[parent] = heap[child];
		parent = child;
		child *= 2;
	}
	heap[parent] = temp;
	--heap_size;
}

bool comp(int a, int b) {
	return (a > b);
}

int main()
{
	int a[10000], b[10000];
	for (int i = 0; i < 9999; ++i) {
		a[i] = rand() % 10000;
		b[i] = a[i];
	}
	sort(a, a + 9999, comp);

	for (int i = 0; i < 9999; ++i) {
		push(b[i]);
	}

	for (int i = 0; i < 9999; ++i) {
		if (a[i] != heap[1]) {
			printf("not heap!!!\n");
		}
		pop();
	}

	return 0;
}


설정

트랙백

댓글