티스토리 뷰

이 문제는 이분탐색 or 시물레이션으로 풀 수있는것같다

주구장창 시물레이션 풀다가 계속 틀렸다

최적화의 계산을 정~~~~말 못해서 그런거같다

 

이분탐색도 생각 못했다.. 최소의 최대체력인데..!!! 문제에 써있는데..!!

최대체력을 mid로 두고 이분탐색을 해서 그 체력이 가능한지 돌려보자!

 

소스코드

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
38
39
40
41
42
43
44
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
ll n,k;
vector<pair<ll,pair<ll,ll>>> vt;
 
bool solve(ll mid){
    ll atk =k;
    ll hp = mid;
    for(int i=0;i<n;i++){
        if(vt[i].first==1){
            ll cnt = vt[i].second.second/atk;
            if(vt[i].second.second%atk) cnt++;
            cnt--;
            hp-=(cnt*vt[i].second.first);
            if(hp<=0return false;
        } else {
            atk+=vt[i].second.first;
            hp+=vt[i].second.second;
            if(hp>mid) hp=mid;
        }
    }
    return true;
}
int main(){
    //freopen("input.txt","r",stdin);
    scanf(" %lld %lld",&n,&k);
    for(int i=0;i<n;i++){
        ll a,b,c; scanf(" %lld %lld %lld",&a,&b,&c);
        vt.push_back({a,{b,c}});
    }
    ll l=1,r=1e18,ans=1e18;
    while(l<=r){
        ll mid = (l+r)/2;
        if(solve(mid)){
            ans = min(ans,mid);
            r = mid-1;
        } else l = mid+1;
    }
    printf("%lld\n",ans);
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

'알고리즘 > BOJ' 카테고리의 다른 글

[백준] 16440 제이크와 케이크  (0) 2019.09.20
[백준] 16437 양 구출 작전  (0) 2019.09.20
[백준] 16432 떡장수와 호랑이  (0) 2019.09.19
[백준] 5052 전화번호 목록  (0) 2019.09.03
[백준] 2751 수 정렬하기 2  (0) 2019.09.02
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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
글 보관함