1. ๋ชจํ๊ฐ ๊ธธ๋
1, 2, 3 / 2 2 ๊ฒฐ์ฑ ๋ผ์ผ ํ๋์ค์๊ณ reserve ์จ์ ํ๋๋ฐ ์ด๋ ๊ฒ ํ ์ ์๊ตฌ๋ ..!
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, num;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num;
v.push_back(num);
}
sort(v.begin(), v.end()); // 1 2 2 2 3
int result = 0; // ์ด ๊ทธ๋ฃน ์
int cnt = 0; //ํ์ฌ ๊ทธ๋ฃน์ ํฌํจ๋ ๋ชจํ๊ฐ ์
for (int i = 0; i < n; i++) {
cnt++;
if (cnt >= v[i]) {
result++;
cnt = 0;
}
}
cout << result;
//1, 2 2 ๊ทธ๋ฃน
//2, 3์ ๊ทธ๋ฃน๊ฒฐ์ฑ ๋ชปํจ ..
}
2. ๊ณฑํ๊ธฐ ํน์ ๋ํ๊ธฐ
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int result = s[0] - '0';
for (int i = 1; i < s.size(); i++) {
int num = s[i] - '0';
if (num == 0 || num == 1) {
result += num;
} else {
result *= num;
}
}
cout << result;
}
3. ๋ฌธ์์ด ๋ค์ง๊ธฐ
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
int cnt0 = 0, cnt1 = 0; //0๋ก ๋ผ์๋ ๊ตฌ๊ฐ, 1๋ก ๋ผ์๋ ๊ตฌ๊ฐ
cin >> s;
if (s[0] == '0') {
cnt0++;
} else {
cnt1++;
}
// 1๋ก ๋ฐ๋๋ ๊ฒฝ์ฐ : 1 / 0์ผ๋ก ๋ฐ๋๋ ๊ฒฝ์ฐ : 2
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] != s[i + 1]) {
if (s[i + 1] == '0') {
cnt0++;
} else {
cnt1++;
}
}
}
if (cnt0 < cnt1) {
cout << cnt0;
} else {
cout << cnt1;
}
}
4. ๋ง๋ค ์ ์๋ ๊ธ์ก
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
v.push_back(num);
}
sort(v.begin(), v.end());
int target = 1;
for (int i = 0; i < v.size(); i++) {
if (target < v[i])
break;
target += v[i];
}
cout << target;
}
5. ๋ณผ๋ง๊ณต ๊ณ ๋ฅด๊ธฐ
2์ฐจ์ ๋ฐฐ์ด๋ก ํด๋ 1์ด๋ผ ๊ทธ๋ ๊ฒ ํ์๋๋ฐ ๋ค๋ฅธ ์ข์ ๋ฐฉ๋ฒ์ด ์์๋ค!
1 3 2 3 2
๋ฌด๊ฒ 1 : 2๊ฐ
๋ฌด๊ฒ 2 : 2๊ฐ
๋ฌด๊ฒ 3 : 2๊ฐ
A ์ ํ B ์ ํ
๋ฌด๊ฒ 1 : 4๊ฐ์ง -> (1, 2), (1, 3), (1, 4), (1, 5) = (5-1)*4)
๋ฌด๊ฒ 2 : 4๊ฐ์ง -> (3, 2), (3, 4), (5, 2), (5, 4) = (4-2)*2
๋ฌด๊ฒ 3 : 0๊ฐ์ง = (2-2)*2
์ ์ฒด ๋ณผ๋ง๊ณต ๊ฐ์์์ ํ์ฌ ๋ณผ๋ง๊ณต์ ๊ฐ์๋ฅผ ๋นผ๊ณ , (A์ ์ ํ์ ๋ฐ๋ผ B ์ ํ์ ํญ์ด ์ข์์ง)
ํ์ฌ ๋ณผ๋ง๊ณต์ ๊ฐ์๋ฅผ ๋ํด์ค ๊ฒ์ ๋์ ํฉํ๋ฉด ๋จ !
#include <iostream>
using namespace std;
int arr[11];
int main() {
int n, m, num, cnt = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> num;
arr[num]++;
}
for (int i = 1; i <= m; i++) {
n -= arr[i];
cnt += n * arr[i];
}
cout << cnt;
}
6. ๋ฌด์ง์ ๋จน๋ฐฉ ๋ผ์ด๋ธ
1. k๊ฐ 0์ผ ๋

2. ์ฐจ์ด๋ฅผ ๋บ ๋, ๋จ์์๊ฐ๋ณด๋ค ์ฐจ์ด๊ฐ ํด ๊ฒฝ์ฐ

์ฌ๊ธฐ์ summary๋ ์์ ๊ทธ๋ฆผ๊ณผ ๋ค๋ฅด๊ฒ ๋์ ํฉ์ผ๋ก k๋ ๋น๊ตํจ !
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
return a.second < b.second;
}
int solution(vector<int> food_times, long long k) {
long long summary = 0;
for (int food_time: food_times) {
summary += food_time;
}
if (summary <= k) { //๋จน์ ๊ฒ ์์
return -1;
}
//์๊ฐ์ด ์์ ์์๋ถํฐ ๋นผ์ผํ๊ธฐ ๋๋ฌธ์ pq ์ฌ์ฉ
priority_queue<pair<int, int>> pq;
for (int i = 0; i < food_times.size(); i++) {
pq.push({-food_times[i], i + 1}); //(์์ ์๊ฐ, ์์ ๋ฒํธ)
}
summary = 0; //๋จน๊ธฐ ์ํด ์ฌ์ฉํ ์๊ฐ
long long pre = 0; //์ง์ ์ ๋ค '๋จน์ ์์' ์๊ฐ
long long length = food_times.size(); //'๋จ์ ์์'์ ๊ฐ์
while (summary + ((-pq.top().first - pre) * length) <= k) { //3 -> 5 -> 6(์ข
๋ฃ)
int now = -pq.top().first;
pq.pop();
summary += (now - pre) * length;
length--;
pre = now;
}
//๋จ์ ์์ ์ค์์ '๋ช ๋ฒ์งธ ์์' ์ธ์ง ํ์ธํด ์ถ๋ ฅ (if ๋จ์์๊ฐ 0์ด๋ฉด, result์ ํ๋๋ง ๋ค์ด๊ฐ)
vector<pair<int, int>> result;
while (!pq.empty()) {
int food_time = -pq.top().first;
int num = pq.top().second;
pq.pop();
result.emplace_back(food_time, num);
}
sort(result.begin(), result.end(), compare); // ์์์ ๋ฒํธ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
return result[(k - summary) % length].second;
}
int main() {
vector<int> food_times = {3, 1, 2};
long long k = 5;
cout << solution(food_times, k); //์์ ๋ฒํธ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
return 0;
}
'โ๏ธAlgorithm > ๐ ์ด์ทจ์ฝ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด์ทจ์ฝ] ์ ํ๋ณ ์๊ณ ๋ฆฌ์ฆ - ์ด์ง ํ์(C++) (0) | 2022.11.04 |
---|---|
[์ด์ทจ์ฝ] ์ ํ๋ณ ์๊ณ ๋ฆฌ์ฆ - ๊ตฌํ(C++) (0) | 2022.11.03 |
[์ด์ทจ์ฝ] 10. ๊ทธ๋ํ ์ด๋ก - C++ (0) | 2022.10.22 |
[์ด์ทจ์ฝ] 9. ์ต๋จ๊ฒฝ๋ก - C++ (0) | 2022.10.22 |
[์ด์ทจ์ฝ] 8. DP - C++ (0) | 2022.10.21 |