PS
BOJ 23889 : 돌 굴러가유
lickelon
2024. 7. 3. 22:45
- 문제 링크 : boj.kr/23889
- 난이도 : G4
- 태그 : 그리디, 정렬
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
bool comp(pii& a, pii& b) {
if(a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<int> arr(n);
for(auto &u : arr) cin >> u;
vector<pii> stone(k+1);
for(int i = 0; i < k; i++) {
cin >> stone[i].second;
}
stone[k].second = n+1;
for(int i = 0; i < k; i++) {
for(int j = stone[i].second-1; j < stone[i+1].second-1; j++) {
stone[i].first += arr[j];
}
}
stone.pop_back();
sort(all(stone), comp);
vector<int> ans(m);
for(int i = 0; i < m; i++) {
ans[i] = stone[i].second;
}
sort(all(ans));
for(auto u : ans) {
cout << u << "\n";
}
return 0;
}
풀이
돌이 굴러오기 시작하는 위치에 벽을 세워야 함은 당연하다.
어떤 위치에 벽을 세울 경우 이 벽은 그 다음 돌이 굴러오는 위치의 앞까지 막을 수 있다.
따라서 돌이 굴러오는 위치를 기준으로 구간을 정할 수 있게 되며, 이 구간 중 고른 구간의 합이 최대가 되도록 m개의 구간을 고르면 된다.
728x90