• Home
  • About
    • Dochoi의 소소한 코딩 모음 photo

      Dochoi의 소소한 코딩 모음

      코딩을 하면서 느낀점들을 모은 공간입니다.

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
    • AI
    • Algorithm
    • Algorithm-Test
    • Cloud
    • Docker
    • Kubernetes
    • iOS
    • Culture
  • Projects

[백준] : 15594(Out of Place)

02 Sep 2020

Reading time ~1 minute

algorithm-test

15594(Out of Place)

#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
#include <algorithm>
#include <string>
using namespace std;

int n;
int main(void)
{
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin >> n;
	int cnt = 0;
	vector<int> v;
	int pre = 0;
	for (int i = 0 ; i < n ; i++)
	{
		int temp;
		cin >> temp;
		if (pre == temp)
			continue;
		pre = temp;
		v.push_back(temp);
	}

	while(true)
	{
		int i;
		for (i = 0; i < v.size() - 1; i++)
		{
			if (v[i] > v[i + 1])
			{
				cnt++;
				int temp = v[i];
				v[i] = v[i + 1];
				v[i + 1] = temp;
				break;
			}
		}
		if (i == v.size() - 1)
			break;
	}
	cout << cnt;
}

고찰

단순 구현문제이다. 연속으로 키가 같은 소를 없애주면 쉽게 풀수 있다.



Algorithm-TestImplement Share Tweet +1