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 | 29 | 30 | 31 |
Tags
- DirectX11
- 2294
- 언리얼엔진5
- UE5
- UnrealEngine4
- algorithm
- const
- 프로그래머스
- baekjoon
- Frustum
- UnrealEngine5
- 팰린드롬 만들기
- IFileDialog
- TObjectPtr
- winapi
- Programmers
- 줄 세우기
- RVO
- GeeksForGeeks
- directx
- softeer
- Unreal Engine5
- 티스토리챌린지
- 1563
- C
- 오블완
- C++
- NRVO
- RootMotion
- 백준
Archives
- Today
- Total
Game Develop
[Algorithm]Baekjoon 1461번 :: 도서관 본문
https://www.acmicpc.net/problem/1461
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_map>
#include <stack>
#include <numeric>
#include <climits>
#include <bitset>
#include <cmath>
using namespace std;
int n, m, a;
vector<int> positiveNums;
vector<int> negativeNums;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
cin >> a;
if (a > 0)
{
positiveNums.push_back(a);
}
else
{
negativeNums.push_back(a);
}
}
sort(positiveNums.rbegin(), positiveNums.rend());
sort(negativeNums.begin(), negativeNums.end());
int answer = 0;
int positiveIndex = 0;
int negativeIndex = 0;
if (negativeNums.size() == 0)
{
answer = positiveNums[0];
positiveIndex = m;
}
else if (positiveNums.size() == 0)
{
answer = negativeNums[0] * -1;
negativeIndex = m;
}
else
{
if (negativeNums[0] * -1 >= positiveNums[0])
{
answer = negativeNums[0] * -1;
negativeIndex = m;
}
else if (positiveNums.size() > 0)
{
answer = positiveNums[0];
positiveIndex = m;
}
}
while (negativeIndex < negativeNums.size())
{
answer += negativeNums[negativeIndex] * -1 * 2;
negativeIndex += m;
}
while (positiveIndex < positiveNums.size())
{
answer += positiveNums[positiveIndex] * 2;
positiveIndex += m;
}
cout << answer;
}
|
cs |
이 문제에서의 중요포인트는 마지막책을 갖다 놓으면 다시 원점위치로 되돌가가지 않아도 되는 것.
그러니 제일 멀리있는 위치를 기준으로 한 m개로 구성된 그룹은 제일 마지막에 가져다 놓으면 된다.
'Algorithm > Algorithm' 카테고리의 다른 글
[Algorithm]Baekjoon 17951번 :: 흩날리는 시험지 속에서 내 평점이 느껴진거야 (0) | 2025.09.18 |
---|---|
[Algorithm]Baekjoon 3079번 :: 입국심사 (0) | 2025.09.17 |
LCS (0) | 2023.11.06 |
MergeSort와 QuickSort (0) | 2022.12.27 |
음수사이클이 있는 그래프 (다익스트라, 벨만포드, 플로이드와샬) (1) | 2022.12.26 |