生活随笔
收集整理的這篇文章主要介紹了
2783: 魔法药水【二分】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
小A終于來到了最后一題。小明翻找自己的背包,發(fā)現(xiàn)了很多能夠提升自己能力值的藥水。小A共發(fā)現(xiàn)了n種藥水,第i種藥水有ai瓶,使用后能使能力值加bi。但相同種類的藥水,是不能連續(xù)使用的。小A覺得自己至少將能力值提升X(X>=1),才有把握AK這場新生賽。藥水的味道并不好,小明想盡量少的使用藥水。你能告訴小A,他至少使用多少瓶藥水, 才能使自己的能力提升至少X?
輸出
如果小A不能使自己的能力提升至少X,輸出“-1”;否則輸出小A最少使用的藥水的數(shù)量。
樣例輸入 Copy
2 1000
10 200
5 1
樣例輸出 Copy
9
來源/分類
鄭州輕工業(yè)大學(xué)2020級新生賽
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <map>
#include <cmath>
#include <set>
#include <time.h>
#include <stdlib.h>
#include <random>#define go(i, l, r) for(ll i = (l), i##end = (ll)(r); i <= i##end; ++i)
#define god(i, r, l) for(ll i = (r), i##end = (ll)(l); i >= i##end; --i)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug_in freopen("in.txt","r",stdin)
#define debug_out freopen("out.txt","w",stdout);
#define pb push_back
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using namespace std
;
typedef long long ll
;
typedef unsigned long long ull
;
typedef __int128 bigint
;
typedef pair
<ll
,ll
> pii
;
const ll inf_int
= 1e9+10;
const ll inf_ll
= 1e17;void read(int &x
){scanf("%d",&x
);
}
void read(ll
&x
){scanf("%lld",&x
);
}
template<class H, class... T
> void read(H
& h
, T
&... t
) {read(h
);read(t
...);
}void pt(){ cout
<<'\n';}
template<class H, class ... T
> void pt(H h
,T
... t
){ cout
<<" "<<h
; pt(t
...);}
const int maxn
= 1e6 + 10;
ll N
,X
;
struct node
{ll a
,b
;bool operator <(const node
& o
) const{return b
> o
.b
;}
}arr
[maxn
];ll
judge(ll mid
){ll Left
= mid
,curx
= X
;for(int i
= 1;i
<=N
;i
++){ll x
= arr
[i
].a
, y
= arr
[i
].b
;ll most
= (mid
+1)/2;ll cnt
= min({x
,most
,Left
});curx
-= cnt
* y
;Left
-= cnt
;if(Left
== 0) break;}if(Left
> 0) return 1;else if(curx
<= 0) return 2;else return 0;
}
ll
solve(){sort(arr
+1,arr
+N
+1);ll l
= 1,r
= 1e18,ans
= -1;while(l
<=r
){ll mid
= (l
+r
)>>1;int res
= judge(mid
);if(res
== 1) r
= mid
-1;else if(res
== 2) r
= mid
-1,ans
= mid
;else l
= mid
+1;}printf("%lld\n",ans
);
}
int main() {
read(N
,X
);for(int i
= 1;i
<=N
;i
++){ll a
,b
;read(a
,b
);arr
[i
] = {a
,b
};}solve();return 0;
}
``
總結(jié)
以上是生活随笔為你收集整理的2783: 魔法药水【二分】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。