Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- BFS
- 수학
- Unreal Engine 5
- XR Interaction Toolkit
- VR
- 누적 합
- 브루트포스
- 시뮬레이션
- 구현
- c++
- 유니온 파인드
- 백준
- 그래프
- 재귀
- 알고리즘
- 문자열
- 백트래킹
- 트리
- 다이나믹 프로그래밍
- 유니티
- Team Fortress 2
- 우선순위 큐
- DFS
- 자료구조
- 다익스트라
- 투 포인터
- 그리디 알고리즘
- 정렬
- 스택
- ue5
Archives
- Today
- Total
1일1알
백준 10431번 줄세우기 C++ 본문
입력받은 순서대로 줄을 세우는데, 작은 수가 앞에 오도록 줄을 세워야 한다. 이때, 이동이 얼마나 일어나는지를 구하는 문제이다.
1. 자신보다 큰 수를 찾는다.
2. 없으면 push_back을 한다.
3. 있으면 중간에 끼워넣고 그 뒤에 있는 수만큼 sum을 증가시킨다.
find_if와 람다를 사용해서 문제를 풀어보았다.
#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;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int p;
cin >> p;
while (p--) {
int t;
cin >> t;
int sum = 0;
vector<int> v;
for (int i = 0; i < 20; i++) {
int num;
cin >> num;
auto it = find_if(v.begin(), v.end(), [=](int val) {return num < val; });
if (it == v.end()) v.push_back(num);
else {
sum += v.end() - it;
v.insert(it, num);
}
}
cout << t << " " << sum << "\n";
}
};
'알고리즘' 카테고리의 다른 글
백준 2016번 미팅 주선하기 C++ (0) | 2022.02.01 |
---|---|
백준 3987번 보이저 1호 C++ (0) | 2022.02.01 |
백준 1491번 나선 C++ (0) | 2022.01.30 |
백준 2777번 숫자 놀이 C++ (0) | 2022.01.29 |
백준 8980번 택배 C++ (0) | 2022.01.28 |