CodeForces 670D2 Magic Powder - 2(二分+贪心)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces 670D2 Magic Powder - 2(二分+贪心)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://codeforces.com/contest/670/problem/D2
簡單的二分,二分所有可以做的餅干數(shù),然后遍歷就可以啦
#include <iostream> #include <stdio.h> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std;typedef long long LL; #define N 210000 #define INF 0x3f3f3f3f #define met(a, b) memset (a, b, sizeof(a))struct node {LL one, sum;///做一個餅干需要的材料和已經(jīng)有的材料LL num, need;///當前材料可以做的餅干數(shù)和做剩下的材料 }p[N];bool cmp (node a, node b) {return a.num < b.num; }LL n, k, L, R;LL Search () {LL l = L, r = R, maxn = 0;while (l<=r){LL mid = (l+r)/2, S = 0;for (int i=0; i<n; i++){if (p[i].num < mid){S += p[i].one-p[i].need;if (mid>p[i].num)S += (mid-1-p[i].num)*p[i].one;if (S > k) break;///第一次沒加上這個判斷條件WA在第128組測試數(shù)據(jù)上了}}if (S <= k){maxn = max (maxn, mid);l = mid+1;}else r = mid-1;}return maxn; }void Solve () {sort (p, p+n, cmp);L = R = p[0].num;for (int i=0; i<n; i++)R = max (R, (k+p[i].need)/p[i].one+p[i].num);LL ans = Search();printf ("%I64d\n", ans); }int main () {while (scanf ("%I64d %I64d", &n, &k) != EOF){met (p, 0);for (int i=0; i<n; i++)scanf ("%I64d", &p[i].one);for (int i=0; i<n; i++){scanf ("%I64d", &p[i].sum);p[i].num = p[i].sum / p[i].one;p[i].need = p[i].sum % p[i].one;}Solve ();}return 0; }
總結(jié)
以上是生活随笔為你收集整理的CodeForces 670D2 Magic Powder - 2(二分+贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长庆油田2021年油气当量突破6000万
- 下一篇: 如何提升计算机的网络性能,提升WIFI信