[BOJ][C++] λ°±μ€ 10025λ²: κ²μΌλ₯Έ λ°±κ³° (Silver III)
https://www.acmicpc.net/problem/10025
10025λ²: κ²μΌλ₯Έ λ°±κ³°
첫 μ€μ μ μ Nκ³Ό Kκ° λ€μ΄μ¨λ€. λμ§Έ μ€λΆν° Nμ§Έ μ€κΉμ§, 곡백μ μ¬μ΄μ λκ³ κ° μλμ΄μ μΌμμ μμ λνλ΄λ giμ μλμ΄μ μ’νλ₯Ό λνλ΄λ xiκ° μ£Όμ΄μ§λ€.
www.acmicpc.net
λ¬Έμ
λμ΄ μ¬λ¦λ λλ¬Όμμ λ°±κ³° μ¨λ²νΈλ λ무 λμμ κΌΌμ§λ νκΈ° μ«λ€. λ€ννλ μ¬μ‘μ¬λ€μ΄ μ¨λ²νΈμ λμλ₯Ό μνκΈ° μν΄ μΌμμ΄ λ΄κΈ΄ μλμ΄λ€μ κ°μ Έλ€ μ£Όμλ€. μ¨λ²νΈκ° κ°μ₯ μ μ κ±°λ¦¬λ§ μμ§μ΄κ³ λ μ΅λν λ§μ μΌμμΌλ‘ λμλ₯Ό μν μ μλλ‘ λμμ£Όμ.
μ°λ¦¬ μμ 1μ°¨μ λ°°μ΄λ‘ μκ°νλ©°, μ΄ N(1 ≤ N ≤ 100000)κ°μ μΌμ μλμ΄λ€μ΄ xi(0 ≤ xi ≤ 1,000,000)μ’νλ§λ€ λμ¬ μκ³ κ° μλμ΄ μμλ gi(1 ≤ gi ≤ 10,000)μ©μ μΌμμ΄ λ€μ΄ μλ€. μΌλ¨ μ¨λ²νΈκ° μ리λ₯Ό μ‘μΌλ©΄ κ·Έλ‘λΆν° μ’μ°λ‘ K(1 ≤ K ≤ 2,000,000) λ§νΌ λ¨μ΄μ§ μλμ΄κΉμ§ λΏμ μ μλ€. μ¨λ²νΈλ μλμ΄κ° λμ¬ μλ μ리μλ μ리μ‘μ μ μλ€. λͺ¨λ μΌμ μλμ΄μ μμΉλ λ€λ₯΄λ€.
μ¨λ²νΈκ° μ΅μ μ μ리λ₯Ό 골λμ λ μΌμμ ν©μ ꡬνμμ€. μ¦, μΌμλ€μ ν©μ μ΅λκ°μ ꡬν΄μΌ νλ€.
μ λ ₯
첫 μ€μ μ μ Nκ³Ό Kκ° λ€μ΄μ¨λ€. λμ§Έ μ€λΆν° Nμ§Έ μ€κΉμ§, 곡백μ μ¬μ΄μ λκ³ κ° μλμ΄μ μΌμμ μμ λνλ΄λ giμ μλμ΄μ μ’νλ₯Ό λνλ΄λ xiκ° μ£Όμ΄μ§λ€.
μΆλ ₯
μ¨λ²νΈκ° νν μ΅μ μμΉλ‘λΆν° Kλ§νΌ λ¨μ΄μ§ 거리 λ΄μ μλ μΌμλ€μ ν©(μ΅λκ°)μ μΆλ ₯νλ€.
νμ΄
// νμ΄ : https://whkakrkr.tistory.com
#include <bits/stdc++.h>
using namespace std;
const int INF = 4000001;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
// input
int n,k, g,x;
cin >> n >> k;
vector<int>v(INF, 0);
for(int i=0; i<n; i++) {
cin >> g >> x;
v[x] = g;
}
// solution
long long cur=0, ans=0;
int left=0, right=0;
for(; right<=2*k; right++) {
cur += v[right];
}
while(right<INF) {
ans = max(cur, ans);
cur -= v[left];
cur += v[right];
left++;
right++;
}
cout << ans;
return 0;
}
2025.04.22
// Authored by : seondal
// Co-authored by : -
// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
const int SIZE = 1000000;
int maxIce(int k, int end_point, vector<int>&position) {
int section = 2*k + 1;
int section_ice = 0;
for(int i=0; i<section; i++){
if(i > end_point)
break;
section_ice += position[i];
}
int ans = section_ice;
for(int i=section; i<=end_point; i++) {
section_ice -= position[i-section];
section_ice += position[i];
ans = max(ans, section_ice);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> position(SIZE+1, 0);
int g, x, end_point=0;
while(n--) {
cin >> g >> x;
position[x] = g;
end_point = max(end_point, x);
}
cout << maxIce(k, end_point, position);
return 0;
}
/*
*/
2021.08.08 ?