二分:chocolate eating

2024-01-23 20:48
文章标签 二分 eating chocolate

本文主要是介绍二分:chocolate eating,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述:链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

Bessie has received N (1 <= N <= 50,000) chocolates from the bulls, but doesn't want to eat them too quickly, so she wants to plan out her chocolate eating schedule for the next D (1 <= D <= 50,000) days in order to maximize her minimum happiness level over the set of those days.
Bessie's happiness level is an integer that starts at 0 and halves (rounding down if necessary) over night as she sleeps. However, when she eats chocolate i, her happiness level increases by integer HiH_iHi​ (1 <= HiH_iHi​ <= 1,000,000). If she eats chocolates on a day, her happiness for that day is considered the happiness level after she eats the chocolates. Bessie insists that she eat the chocolates in the order that she received them.
If more than one optimal solution exists, print any one of them.
Consider a sequence of 5 chocolates to be eaten over a period of 5 days; they respectively bring happiness (10, 40, 13, 22, 7).

If Bessie eats the first chocolate (10 happiness) on the first day and then waits to eat the others, her happiness level is 10 after the first day.

Here is the complete schedule which turns out to maximize her minimum happiness:Day  Wakeup happiness   Happiness from eating   Bedtime happiness1            0                10+40                  502           25                 ---                   253           12                  13                   254           12                  22                   34 5           17                   7                   24
The minimum bedtime happiness is 24, which turns out to be the best Bessie can do.

输入描述:

* Line 1: Two space separated integers: N and D
* Lines 2..N+1: Line i+1 contains a single integer: HiH_iHi​

输出描述:

* Line 1: A single integer, the highest Bessie's minimum happiness can be over the next D days
* Lines 2..N+1: Line i+1 contains an integer that is the day on which Bessie eats chocolate i

示例1

输入

5 5 
10 
40 
13 
22 
7 

输出

24
1
1
3
4
5

大意描述:她有n块巧克力,要在k天内吃完,并且吃的顺序已经给定,每块巧克力可以给她带来一定的幸福值,而且每天晚上她的幸福值会减半,求她的最小幸福值的最大值,并给出每块巧克力是在第几天被吃掉的。

解析:

求最小值的最大值,这是典型的二分思路,那么我们可以直接对答案进行二分,对于每次判断,使用一个while循环,即当sum(累计幸福值)还小于所二分的结果时,就不断喂她吃巧克力,直到她的sum>=二分结果。对于巧克力被吃的记录,可以另开应该数组,当吃掉它的时候,就将它记录,这并不是难事,关键就在于判断应该怎么写了。

但是,还有几个小点要注意(我为此wa了好几次。。。)。    首先是关于判断函数该在哪些情况下返回false:所有巧克力都吃完了,但没有撑到最后一天,或者撑到了最后一天,但最后一天的幸福值没有达到要求,那么就要返回false。        其次,当巧克力过多,即吃到最后,幸福值也够了的时候,巧克力还有剩,那么对于剩下的巧克力,我们应该在最后一天全部吃掉,因为题目要求输出的是“每块巧克力是在第几天被吃掉的。”      以及,开二分的时候,上界要取够,可以用一个ans求出所有巧克力幸福值的总和作为上界,也可以直接long long 一个1e15;  最后,当二分循环已经求出答案时,我们应该在循环外再进行一次判断,因为我们的判断函数,除了判断的功能之外,还要记录每块巧克力被吃的日子,在最后重新进行一次这个操作,即对答案再记录一次每块巧克力被吃的日子,那么就可以保证其正确性,因为在二分循环中进行的若干次操作,会对记录的数组进行错误篡改。

上代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,d,mas[50010],r=1e16,l=0,k[50010];
bool judge(ll x){//每天最小开心值为x; //memset(k,0,sizeof(k));ll sum=0;ll zhen=1,i=1;for(;i<=d;++i){//d天 while(sum<x&&zhen<=n){k[zhen]=i;sum+=mas[zhen++];}if(sum<x) return false;sum>>=1;}while(zhen<=n) k[zhen++]=d; //点1.。。//if(i<d) return false;return true;
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin>>n;cin>>d;//n块巧克力,k天吃完; for(int i=1;i<=n;++i) cin>>mas[i];while(l<=r){ll mid=(l+r)>>1;if(judge(mid)) l=mid+1;else r=mid-1;}cout<<l-1<<endl;judge(l-1);//点2.。。。。for(int i=1;i<=n;++i) cout<<k[i]<<endl;return 0;
}

这篇关于二分:chocolate eating的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/637480

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

poj 3258 二分最小值最大

题意: 有一些石头排成一条线,第一个和最后一个不能去掉。 其余的共可以去掉m块,要使去掉后石头间距的最小值最大。 解析: 二分石头,最小值最大。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <c

poj 2594 二分图最大独立集

题意: 求一张图的最大独立集,这题不同的地方在于,间接相邻的点也可以有一条边,所以用floyd来把间接相邻的边也连起来。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <sta

poj 3692 二分图最大独立集

题意: 幼儿园里,有G个女生和B个男生。 他们中间有女生和女生认识,男生男生认识,也有男生和女生认识的。 现在要选出一些人,使得这里面的人都认识,问最多能选多少人。 解析: 反过来建边,将不认识的男生和女生相连,然后求一个二分图的最大独立集就行了。 下图很直观: 点击打开链接 原图: 现图: 、 代码: #pragma comment(

poj 2112 网络流+二分

题意: k台挤奶机,c头牛,每台挤奶机可以挤m头牛。 现在给出每只牛到挤奶机的距离矩阵,求最小化牛的最大路程。 解析: 最大值最小化,最小值最大化,用二分来做。 先求出两点之间的最短距离。 然后二分匹配牛到挤奶机的最大路程,匹配中的判断是在这个最大路程下,是否牛的数量达到c只。 如何求牛的数量呢,用网络流来做。 从源点到牛引一条容量为1的边,然后挤奶机到汇点引一条容量为m的边

二分最大匹配总结

HDU 2444  黑白染色 ,二分图判定 const int maxn = 208 ;vector<int> g[maxn] ;int n ;bool vis[maxn] ;int match[maxn] ;;int color[maxn] ;int setcolor(int u , int c){color[u] = c ;for(vector<int>::iter

POJ2413二分

注意二分, 上界。 import java.beans.beancontext.BeanContext;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWrite