[삼성 SW 역량 테스트] 미세먼지 안녕!

 

 

 

 

#include <stdio.h>

int r, c, t;

int map[2][50][50];

int up_y, up_x, down_y, down_x;

const int dy[] = { -1, +1, 0, 0 };
const int dx[] = { 0, 0, -1, +1 };

void spead(int cur) {
	int next = (cur + 1) % 2;
	for (int y = 0; y < r; ++y) {
		for(int x = 0; x < c; ++x) {
			if (map[cur][y][x] == -1) {
				map[next][y][x] = -1;
			}
			else {
				map[next][y][x] = 0;
			}
		}
	}

	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			int val = (map[cur][y][x] / 5);
			int candi = 0;
			for (int dir = 0; dir < 4; ++dir) {
				int ny = y + dy[dir];
				int nx = x + dx[dir];

				if (ny < 0 || ny >= r || nx < 0 || nx >= c) {
					continue;
				}

				if(map[next][ny][nx] != -1) {
					map[next][ny][nx] += val;
					candi += val;
				}
			}

			if (map[next][y][x] != -1) {
				map[next][y][x] += (map[cur][y][x] - candi);
			}
		}
	}
}

void move(int cur) {
	for (int y = up_y - 1; y > 0; --y) {
		map[cur][y][0] = map[cur][y - 1][0];
	}

	for (int x = 0; x < c - 1; ++x) {
		map[cur][0][x] = map[cur][0][x + 1];
	}

	for (int y = 0; y < up_y; ++y) {
		map[cur][y][c - 1] = map[cur][y + 1][c - 1];
	}

	for (int x = c - 1; x > 1; --x) {
		map[cur][up_y][x] = map[cur][up_y][x - 1];
	}
	
	map[cur][up_y][1] = 0;


	for (int y = down_y + 1; y < r; ++y) {
		map[cur][y][0] = map[cur][y + 1][0];
	}

	for (int x = 0; x < c - 1; ++x) {
		map[cur][r - 1][x] = map[cur][r - 1][x + 1];
	}

	for (int y = r - 1; y > down_y; --y) {
		map[cur][y][c - 1] = map[cur][y - 1][c - 1];
	}

	for (int x = c - 1; x > 1; --x) {
		map[cur][down_y][x] = map[cur][down_y][x - 1];
	}

	map[cur][down_y][1] = 0;

}

int main()
{
	up_y = -1;
	scanf("%d %d %d", &r, &c, &t);
	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			scanf("%d", &map[0][y][x]);
			if (map[0][y][x] == -1) {
				if (up_y == -1) {
					up_y = y, up_x = x;
				}
				else {
					down_y = y, down_x = x;
				}
			}
		}
	}

	int cur = 0;
	for (int time = 0; time < t; ++time) {
		spead(cur);
		cur = (cur + 1) % 2;
		move(cur);
	}

	int ret = 0;
	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			if (map[cur][y][x] != -1) {
				ret += map[cur][y][x];
			}
		}
	}

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

	return 0;
}

설정

트랙백

댓글

Linked List

Data Structure 2019. 2. 13. 23:00


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

struct NODE {
	int prev;
	int next;
	int val;
};

const int NODE_SIZE = 30000;

//TEST CMD
const int PUSH_BACK = 0;
const int PUSH_FRONT = 1;
const int INSERT = 2;
const int POP_BACK = 3;
const int POP_FRONT = 4;
const int ERASE = 5;

int test_cmd[NODE_SIZE][3];

struct MY_LIST {
	int HEAD = NODE_SIZE;
	int TAIL = NODE_SIZE + 1;
	int pos;
	NODE node[NODE_SIZE + 2];

	MY_LIST() {
		pos = 0;
		node[HEAD].next = TAIL;
		node[TAIL].prev = HEAD;
	}

	void push_back(int data) {
		int prev = node[TAIL].prev;
		int next = node[prev].next;	// TAIL;

		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void push_front(int data) {
		int next = node[HEAD].next;
		int prev = node[next].prev;	// HEAD
		
		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void insert(int p, int data) {
		int next = node[HEAD].next;
		for(int i = 0; i < p; ++i) {
			next = node[next].next;
		}
		int prev = node[next].prev;
	
		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void pop_back() {
		int target = node[TAIL].prev;

		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}

	void pop_front() {
		int target = node[HEAD].next;

		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}

	void erase(int p) {
		int target = node[HEAD].next;
		for (int i = 0; i < p; ++i) {
			target = node[target].next;
		}
		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}
};

MY_LIST my_list;
list<int> stl_list;

int main()
{
	// make test case..
	int cur_size = 0;
	for (int i = 0; i < NODE_SIZE; ++i) {
		if (i < NODE_SIZE / 3) {
			test_cmd[i][0] = rand() % 2;
		}
		else {
			test_cmd[i][0] = rand() % 6;
		}

		switch (test_cmd[i][0]) {
		case PUSH_BACK:
		case PUSH_FRONT: {
			test_cmd[i][1] = rand();
			++cur_size;
			break;
		}
		case INSERT: {
			test_cmd[i][1] = rand() % cur_size;
			test_cmd[i][2] = rand();
			++cur_size;
			break;
		}
		case POP_BACK:
		case POP_FRONT: {
			--cur_size;
			break;
		}
		case ERASE: {
			test_cmd[i][1] = rand() % cur_size;
			--cur_size;
			break;
		}
		}
	}

	// test my list
	int my_list_begin = GetTickCount();
	for (int i = 0; i < NODE_SIZE; ++i) {
		switch (test_cmd[i][0]) {
		case PUSH_BACK: {
			my_list.push_back(test_cmd[i][1]);
			break;
		}
		case PUSH_FRONT: {
			my_list.push_front(test_cmd[i][1]);
			break;
		}
		case INSERT: {
			my_list.insert(test_cmd[i][1], test_cmd[i][2]);
			break;
		}

		case POP_BACK: {
			my_list.pop_back();
			break;
		}
		case POP_FRONT: {
			my_list.pop_front();
			break;
		}
		case ERASE: {
			my_list.erase(test_cmd[i][1]);
			break;
		}
		}
	}
	int my_list_end = GetTickCount();

	// test stl list
	int stl_list_begin = GetTickCount();
	for (int i = 0; i < NODE_SIZE; ++i) {
		switch (test_cmd[i][0]) {
		case PUSH_BACK: {
			stl_list.push_back(test_cmd[i][1]);
			break;
		}
		case PUSH_FRONT: {
			stl_list.push_front(test_cmd[i][1]);
			break;
		}
		case INSERT: {
			list<int>::iterator it = stl_list.begin();
			for (int k = 0; k < test_cmd[i][1]; ++k) {
				++it;
			}
			stl_list.insert(it, test_cmd[i][2]);
			break;
		}

		case POP_BACK: {
			stl_list.pop_back();
			break;
		}
		case POP_FRONT: {
			stl_list.pop_front();
			break;
		}
		case ERASE: {
			list<int>::iterator it = stl_list.begin();
			for (int k = 0; k < test_cmd[i][1]; ++k) {
				++it;
			}
			stl_list.erase(it);
			break;
		}
		}
	}
	int stl_list_end = GetTickCount();

	//time compare
	printf("my list : %d\n", (my_list_end - my_list_begin));
	printf("stl list : %d\n", (stl_list_end - stl_list_begin));

	//result test
	list<int>::iterator it = stl_list.begin();
	int cur = my_list.node[my_list.HEAD].next;
	while (it != stl_list.end()) {
		if (*it != my_list.node[cur].val) {
			printf("Error\n");
		}
		++it;
		cur = my_list.node[cur].next;
	}

	return 0;
}


설정

트랙백

댓글

Merge Sort

Data Structure 2019. 2. 10. 23:43



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

const int MAX_SIZE = 500000;

int arr_size;
int ms[MAX_SIZE], qs[MAX_SIZE], stls[MAX_SIZE], buf[MAX_SIZE];

void merge_sort(int* p, int len) {
	if (len < 2)	return;
	int i, j, k, mid;
	mid = (len / 2);
	i = 0, j = mid, k = 0;

	merge_sort(p, mid);
	merge_sort((p + mid), (len - mid));

	while (i < mid && j < len) {
		if (p[i] < p[j]) {
			buf[k++] = p[i++];
		}
		else {
			buf[k++] = p[j++];
		}
	}

	while (i < mid) {
		buf[k++] = p[i++];
	}

	while (j < len) {
		buf[k++] = p[j++];
	}

	for (int i = 0; i < len; ++i) {
		p[i] = buf[i];
	}
}

void qsort(int* p, int left, int right) {
	if (left >= right)	return;
	int l = left - 1;
	int r = right + 1;
	int mid = p[(l + r) / 2];
	while (1) {
		while (p[++l] < mid);
		while (p[--r] > mid);
		if (l >= r)	break;
		int temp = p[l];
		p[l] = p[r];
		p[r] = temp;
	}
	qsort(p, left, l - 1);
	qsort(p, r + 1, right);
}


int main()
{
	arr_size = MAX_SIZE;

	for (int i = 0; i < arr_size; ++i) {
		ms[i] = rand();
		qs[i] = stls[i] = ms[i];
	}

	int quick_sort_begin = GetTickCount();
	qsort(qs, 0, arr_size - 1);
	int quick_sort_end = GetTickCount();

	int merge_sort_begin = GetTickCount();
	merge_sort(ms, arr_size);
	int merge_sort_end = GetTickCount();

	int stl_sort_begin = GetTickCount();
	sort(stls, stls + arr_size);
	int stl_sort_end = GetTickCount();

	printf("my quick sort : %d\n", (quick_sort_end - quick_sort_begin));
	printf("my merge sort : %d\n", (merge_sort_end - merge_sort_begin));
	printf("stl sort : %d\n", (stl_sort_end - stl_sort_begin));
	printf("=====================================\n");

	quick_sort_begin = GetTickCount();
	qsort(qs, 0, arr_size - 1);
	quick_sort_end = GetTickCount();

	merge_sort_begin = GetTickCount();
	merge_sort(ms, arr_size);
	merge_sort_end = GetTickCount();

	stl_sort_begin = GetTickCount();
	sort(stls, stls + arr_size);
	stl_sort_end = GetTickCount();

	printf("my quick sort : %d\n", (quick_sort_end - quick_sort_begin));
	printf("my merge sort : %d\n", (merge_sort_end - merge_sort_begin));
	printf("stl sort : %d\n", (stl_sort_end - stl_sort_begin));
	printf("=====================================\n");

	for (int i = 0; i < arr_size; ++i) {
		if (qs[i] != stls[i] || ms[i] != stls[i]) {
			printf("Error\n");
		}
	}

	return 0;
}


설정

트랙백

댓글

[KOI]전국본선 2014 중등부 세번째 문제






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

const int MAX_SIZE = 500000;

struct BUS{
	int s, e, index;
};

bool comp(BUS a, BUS b) {
	return (a.s == b.s) ? (a.e > b.e) : (a.s < b.s);
}

bool comp1(BUS a, BUS b) {
	return (a.index < b.index);
}

BUS bus[MAX_SIZE];

int main()
{
	int N, M, end_point = 0;
	scanf("%d %d", &N, &M);
	for(int i = 0; i < M; ++i){
		scanf("%d %d", &bus[i].s, &bus[i].e);
		bus[i].index = i + 1;

		if (bus[i].s > bus[i].e) {
			end_point = max(end_point, bus[i].e);
			bus[i].e += N;
		}
	}

	sort(bus, bus + M, comp);

	deque<BUS> q;
	for (int i = 0; i < M; ++i) {
		if (q.empty() || q.back().e < bus[i].e) {
			q.push_back(bus[i]);
		}
	}

	while (!q.empty() && q.front().e <= end_point) {
		q.pop_front();
	}

	sort(q.begin(), q.end(), comp1);

	for (auto x : q) {
		printf("%d ", x.index);
	}

	return 0;
}


설정

트랙백

댓글

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




#include <stdio.h>

int seat[2000][2001];

int gcd(int a, int b) {
	return (b) ? gcd(b, a % b) : (a);
}

int main()
{
	int ret = 0;
	int from, to;
	scanf("%d %d", &from, &to);
	for (int i = from; i <= to; ++i) {
		for (int j = 0; j < i; ++j) {
			int g = gcd(i, j);
			int up = j / g;
			int down = i / g;
			if (seat[up][down] == 0) {
				seat[up][down] = 1;
				++ret;
			}
		}
	}
	printf("%d\n", ret);
	return 0;
}


설정

트랙백

댓글