λ¬Έμ λ§ν¬
24887λ²: μ΅λνμ ν΄μ (acmicpc.net)
24887λ²: μ΅λνμ ν΄μ
Amelμ SKH νμ¬ λ΄μμ μΌμ μνκΈ°λ‘ μλ¬Έλ μ λ₯ν μ¬μμ΄λ€. κ·Έλ¬λ, 체λ ₯μ΄ λ§€μ° μ μ’μ Amelμ ν루 λμ μΌνκ³ λλ©΄ λΉ λ₯΄κ² μ§μ³λ²λ¦¬κΈ° λλ¬Έμ ν λ² μΌνκ³ λ ν κ°λ₯ν μ€λ κΈ°κ° λμ
www.acmicpc.net
μκ³ ν λν DIvision2 Eλ² λ¬Έμ .
λν λΉμΌμ μμμ λ§νλ λ°λμ μ½μ΄λ³΄μ§λ λͺ»νλ€ γ γ ;
νμ΄
μ°μμΌλ‘ μ° λ μ μ΅μκ°μ μ΅λκ°μ ꡬνλ λ¬Έμ λ€. μ°μμΌλ‘ μ¬λλ μ μ΅μκ°μ μ΄λΆνμμΌλ‘ μ°Ύμμ€λ€.
μ΄κΈ° λ²μλ Left = 0, Right = N μΌλ‘ μ€μ ν΄μ€¬λ€.
DPλ°°μ΄μ μ μΈν΄μ DP[i] μ°μμΌλ‘ μ¬λλ μ μ΅μκ° mid μΌλ, iλ²μ§Έ λ κΉμ§ μ±μΈμ μλ ν λΉλμ μ΅λκ°μ μ μ₯ν΄λ μλ€. κ·Έλμ DP[N] κΉμ§ ꡬν΄μ€λ€ DP[N] >= M(ν λΉλ) μ΄λ©΄ midκ° μ ν¨ν κ²μ΄λ€.
if(DP[N] >= M)
L = mid + 1;
else R = mid - 1;
μν©μ κ°μ ν΄λ³΄μ. νμ¬ mid κ°μ΄ 3μ΄λΌλ κ²μ μ°μμΌλ‘ μ΄ μ μλ λ μ μ΅μκ°μ΄ 3μΌμ΄λΌλ λ»μ΄λ€.
νμ¬ i = 5 μ΄λ€ DP[5] λ₯Ό μ±μ°κ³ μλ€. 5λ²μ§Έ λ μλ μΌμ ν μλ μκ³ μν μλ μλ€.
κ·Έλ κΈ° λλ¬Έμ 2κ°μ§ κ²½μ°λ₯Ό λΉκ΅ν΄μΌνλ€.
1. λ§μ½ μΌμ νλ€λ©΄, μ΅μ 3μΌμ μ¬μ΄μΌνκΈ° λλ¬Έμ 4μΌ μ μΈ μ²«λ²μ§Έλ κΉμ§ μ±μΈ μ μλ μ΅λ ν λΉλ + 5λ²μ§Έ λ μ±μΈ μ μλ ν λΉλ.
2. λ§μ½ μΌμ μνλ€λ©΄ λ°λ‘ μ λ κΉμ§ μ±μΈ μ μλ ν λΉλμ μ΅λμΈ DP[4] λ₯Ό κ·Έλλ‘ κ°μ Έμ¨λ€.
DP[5] = max(DP[4], Work[5] + DP[1]) μ΄ λμ΄μΌνλ€.
μ¦, DP[i - 1]κ³Ό DP[i - mid - 1]μ λΉκ΅ν΄μ λ ν° κ°μ DP[i]μ μ μ₯νλ€.
λ¬Όλ‘ i - mid - 1μ΄ 0λ³΄λ€ μμμ§λ κ²½μ°μλ μμΈλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef priority_queue<pair<pair<int, int>, pair<pair<int, int>, bool>>, vector<pair<pair<int, int>, pair<pair<int, int>, bool>>>, greater<pair<pair<int, int>, pair<pair<int, int>, bool>>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 200000 + 5;
int N, M;
int W[MAX_N];
ll DP[MAX_N];
int Binary_search(int L, int R) {
int ans = 0;
while (L <= R) {
int mid = (L + R) / 2;
for (int i = 1; i <= N; i++) {
DP[i] = W[i];
DP[i] = max(DP[i], DP[i-1]);
if (i > mid)
DP[i] = max(DP[i], (ll)W[i] + DP[i - mid - 1]);
}
if (DP[N] >= M) {
ans = max(ans, mid);
L = mid + 1;
continue;
}
else {
R = mid - 1;
}
}
return ans;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
ll sum = 0;
int Max = 0;
for (int i = 1; i <= N; i++) {
cin >> W[i];
sum += W[i];
Max = max(Max, W[i]);
}
if (Max >= M) {
cout << "Free!\n";
return 0;
}
else if (sum < M) {
cout << -1 << "\n";
return 0;
}
cout << Binary_search(0, N) << "\n";
return 0;
}
|
cs |
μ΄λ²μλ λ²μλ₯Ό μ μκ°νμ§ λͺ»νκ³ long longμ΄ μλ intλ‘ μ μΈν΄μ κ³μ νλ Έμ΅λλ€λ₯Ό λ°μλ€. κ·Έλ λ€κ³ λ§€λ² long longμΌλ‘ μ μΈν μλ μλ€. μΈμ μ―€ long long μΈκ±Έ λ°λ‘ μΈμ§ν μ μμκΉ.
'λ°±μ€ > Gold' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 1915 : κ°μ₯ ν° μ μ¬κ°ν (0) | 2022.04.04 |
---|---|
[BOJ] 24888 : λ ΈνΈ μ‘°κ° (0) | 2022.03.31 |
[BOJ] 24885 : μ£Όμ (0) | 2022.03.29 |
[BOJ] 17404 : RGB거리 2 (0) | 2022.03.22 |
[BOJ] 15823 : μΉ΄λ ν© κ΅¬λ§€νκΈ° (0) | 2022.03.21 |