Skip to content

Commit 63774f7

Browse files
committed
📝 post BOJ solution
1 parent 52c61eb commit 63774f7

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
layout: single
3+
title: "[백준 17931] Greedily Increasing Subsequence (C#, C++) - soo:bak"
4+
date: "2026-01-26 21:41:00 +0900"
5+
description: "백준 17931번 C#, C++ 풀이 - 가장 왼쪽에서부터 더 큰 값을 고르는 GIS를 구하는 문제"
6+
tags:
7+
- 백준
8+
- BOJ
9+
- 17931
10+
- C#
11+
- C++
12+
- 알고리즘
13+
- 구현
14+
keywords: "백준 17931, 백준 17931번, BOJ 17931, Greedily Increasing Subsequence, C# 풀이, C++ 풀이, 알고리즘"
15+
---
16+
17+
## 문제 링크
18+
[17931번 - Greedily Increasing Subsequence](https://www.acmicpc.net/problem/17931)
19+
20+
## 설명
21+
첫 원소를 g₁으로 두고, 이후에는 바로 앞에 뽑은 값보다 큰 값 중 가장 왼쪽에 있는 원소를 차례로 고르는 GIS(Greedily Increasing Subsequence)를 구하는 문제입니다. 더 이상 큰 값이 없으면 종료합니다.
22+
23+
<br>
24+
25+
## 접근법
26+
배열을 한 번 순회하며 직전에 선택한 값 last를 저장합니다.
27+
28+
처음 원소를 무조건 선택하고, 이후 원소가 last보다 크면 결과에 추가하고 last를 갱신합니다.
29+
30+
<br>
31+
32+
- - -
33+
34+
## Code
35+
36+
### C#
37+
```csharp
38+
using System;
39+
using System.Text;
40+
41+
class Program {
42+
static void Main() {
43+
var input = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
44+
var idx = 0;
45+
var n = int.Parse(input[idx++]);
46+
47+
var res = new int[n];
48+
var len = 0;
49+
50+
int last = int.Parse(input[idx++]);
51+
res[len++] = last;
52+
for (int i = 1; i < n; i++) {
53+
int v = int.Parse(input[idx++]);
54+
if (v > last) {
55+
res[len++] = v;
56+
last = v;
57+
}
58+
}
59+
60+
var sb = new StringBuilder();
61+
sb.Append(len).Append('\n');
62+
for (int i = 0; i < len; i++) {
63+
if (i > 0) sb.Append(' ');
64+
sb.Append(res[i]);
65+
}
66+
Console.Write(sb.ToString());
67+
}
68+
}
69+
```
70+
71+
### C++
72+
```cpp
73+
#include <bits/stdc++.h>
74+
using namespace std;
75+
76+
int main() {
77+
ios::sync_with_stdio(false);
78+
cin.tie(nullptr);
79+
80+
int n; cin >> n;
81+
82+
vector<int> ans;
83+
ans.reserve(n);
84+
85+
int last; cin >> last;
86+
ans.push_back(last);
87+
for (int i = 1; i < n; i++) {
88+
int v; cin >> v;
89+
if (v > last) {
90+
ans.push_back(v);
91+
last = v;
92+
}
93+
}
94+
95+
cout << ans.size() << "\n";
96+
for (size_t i = 0; i < ans.size(); i++) {
97+
if (i) cout << ' ';
98+
cout << ans[i];
99+
}
100+
cout << "\n";
101+
102+
return 0;
103+
}
104+
```

0 commit comments

Comments
 (0)