πŸ•οΈ PS (BOJ)/Two Pointer

[BOJ][C++] λ°±μ€€ 10025번: 게으λ₯Έ λ°±κ³° (Silver III)

선달 2022. 10. 18. 15:48
λ°˜μ‘ν˜•

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 ?

λ°˜μ‘ν˜•