1일1알

백준 2872번 우리집엔 도서관이 있어 C++ 본문

알고리즘

백준 2872번 우리집엔 도서관이 있어 C++

영춘권의달인 2022. 1. 24. 12:25

출처 : https://www.acmicpc.net/problem/2872

 

전체 책의 갯수에서 이미 정렬되어있는 책의 갯수를 빼면 된다.

예를 들어 책이 6권 있고, 4 1 5 2 6 3 이런 식으로 되어 있다면, 4, 5, 6은 이미 정렬된 상태이기 때문에 1, 2, 3을 빼서 위에 놓으면 된다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>

using namespace std;
typedef long long ll;

int n;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	vector<int> v(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> v[i];
	}
	int cnt = 0;
	int target = n;
	for (int i = n; i > 0; i--) {
		if (v[i] == target) {
			cnt++;
			target--;
		}
	}
	cout << n - cnt;
};

'알고리즘' 카테고리의 다른 글

백준 1092번 배 C++  (0) 2022.01.26
백준 1474번 밑 줄 C++  (0) 2022.01.25
백준 2891번 카약과 강풍 C++  (0) 2022.01.23
백준 5747 Odd or Even C++  (0) 2022.01.23
백준 2636번 치즈 C++  (0) 2022.01.22