Complete Building the Houses

本文探讨了一道关于房屋建设的贪心算法问题。熊拥有一个空地用于建造一排房子,工人需并肩工作,每日最多建造m座房屋且每次增加高度为1。文章介绍了解决方案,通过扫描房屋并利用贪心策略确保所有房屋高度不低于设计值。

Bear has a large, empty ground for him to build a home. He decides to build a row of houses, one after another, say n in total.

The houses are designed with different height. Bear has m workers in total, and the workers must work side by side. So at a time bear can choose some continuous houses, no more than m, and add their heights by one, this takes one day to finish.

Given the designed height for each house, what is the minimum number of days after which all the houses’ heights are no less than the original design?

Input

The first line of input contains a number T, indicating the number of test cases. (T50)

For each case, the first line contains two integers n and m: the number of houses and the number of workers. The next line comes with nnon-negative numbers, they are the heights of the houses from left to right. (1n,m100,000, each number will be less than 1,000,000,000)

Output

For each case, output Case #i: first. (i is the number of the test case, from 1 to T). Then output the days when bear’s home can be built.

Sample Input

["2\n3 3\n1 2 3\n3 3\n3 2 1"]

Sample Output

["Case #1: 3\nCase #2: 3"]


题目的意思就是 见房子,工人可以一起工作, 而且 最多只能每天造m个房子, 且每个房子的增加高度只能是1. 故 该题就是一道贪心的题目了~从左边开始向右边扫描,~~

注意 时间的定义要用 long long 来 且输出得用 lld~~~~、

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;		
int s[1000010];
int gao[1000010];
int main()
{
	int bbs,i,j,k=1;
	scanf("%d",&bbs);
	while(bbs--)
	{
		long long ant=0;
		int n,m,d;
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
		{
			scanf("%d",&s[i]);
		}
		memset(gao,0,sizeof(gao));
		for(i=1;i<=n;i++)
		{
			if(i-1>=1) gao[i]+=gao[i-1];
			if(s[i]>gao[i])
			{
				 d=s[i]-gao[i];
				 gao[i]+=d;
				 ant+=d;
				 if(i+m<=n) gao[i+m]=-d;
			}
		}
		printf("Case #%d: %lld\n",k++,ant);
	} 

    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值