cdoj 04 Complete Building the Houses 暴力

Complete Building the Houses

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/3

Description

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. (T≤50)

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 n non-negative numbers, they are the heights of the houses from left to right. (1≤n,m≤100,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
3 3
1 2 3
3 3
3 2 1

Sample Output

Case #1: 3
Case #2: 3

HINT

题意

你想修n栋房子,高度分别为a[i],你一次最多可以同时修m栋连在一起的房子,然后问你最少多久能修完这些房子

题解:

暴力做就好了,直接从最左边考虑然后搞搞搞就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200000
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

ll dp[maxn];
void solve()
{
    ll n,m,a,sum=0,ans=0;
    n=read(),m=read();
    for(int i=0;i<n;i++)
    {
        a=read();
        if(i>=m)sum-=dp[i-m];
        dp[i]=0;
        if(a>sum)
        {
            ans+=a-sum;
            dp[i]=a-sum;
            sum=a;
        }
    }
    cout<<ans<<endl;
}
int main()
{
    //test;
    int t=read();
    for(int cas=1;cas<=t;cas++)
    {
        printf("Case #%d: ",cas);
        solve();
    }
}
时间: 2024-07-28 13:40:24

cdoj 04 Complete Building the Houses 暴力的相关文章

UESTC 4 Complete Building the Houses 树状数组

题目来源: http://acm.uestc.edu.cn/#/problem/show/4 分析:就是一个很普通的区间修改,单点查询的树状数组,但是今天忘记吃药了,一直写不对,中午迷迷糊糊地,直接把数据读入到数组里而不是update,然后又总是考虑后面的数被减到0以下要怎么处理,其实根本不用考虑,直接判断大于0就行了,要是修改那些值,就无法满足数组a[]的性质了(a[]是原式的做差,a[i] = x[i] - x[i-1], a[1] = x[1]).想想还是贴出来纪念一下(这有什么好纪念的?

【笔记】网易微专业-Web安全工程师-04.WEB安全实战-2.暴力破解

KP君之前买了一个拉杆箱,在初始设置密码时不熟悉步骤,一时手抖,密码已经设好,但不知道设置了什么密码,欲哭无泪.想要找回密码,只能一个个试验,拉杠箱的密码锁有3位,对应000~999,那么最多需要1000次就能打开密码,这就是简单的"暴力破解". 暴力破解(Brute Force):核心就是"穷举法",猜出用户的密码.看起来似乎工程量很大,但是通常用户设置密码都不太复杂,因此利用常用的密码字典,就能破获大部分的密码.理论上来说,只要给定足够的时间,暴力破解就一定能破

好几年才收集到的软件,分享给大家。。。

QQ:365543212Email:[email protected]请按Clrt+F查找,输入软件关键字查询(不要输入版本号),如果找不到,您可以咨询客服.................FD......................12D MODEL 7.0 规划设计232Analyzer v4.1 高级串口分析监测3D Home Architect Design Suite Deluxe 8.0 室内装潢3D Profiler Tools 11.2 For Archicad 113D R

win10下pip3安装tesserocr时报错

使用pip3在线安装tesserocr时报错,刚开始报错内容是提示未安装vs2014,安装完以后报错内容如下 ERROR: Command errored out with exit status 1: command: 'c:\users\administrator\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"

hdu 4296 Buildings 贪心算法 今日首A 详细解析 ,有些数据类型最好用long long

Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2278    Accepted Submission(s): 870 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

OpenWrt中对USB文件系统的操作, 以及读写性能测试

参考 http://h-wrt.com/en/doc/flash 1. 查看usb存储在启动日志中的信息 # dmesg [ 5.720000] usbcore: registered new interface driver usbfs [ 5.730000] usbcore: registered new interface driver hub [ 5.740000] usbcore: registered new device driver usb [ 5.740000] ehci_hc

寒假练习赛总结

1.18 练习赛第一场.第一场比赛状态很不好,很多人不到3小时就AK,而我因为B.C两题题目读错导致心态变差,最后只A了4道题.可见读题十分重要,如果一下子做不出来,或是WA了,却又有很多人AC,那么切记重新读题,千万不能漏了关键信息. 1.19 Codeforces Round #392 (Div. 2) 练习赛第一场失误,心情不好,因此想好好打一场.比赛时A了4道.A.B.C三题模拟,D题贪心.但不知为什么D题FST了.第二天一看,发现那个数据十分恶心.还好我Rating上升了不少.至此,C

10.32/10.33 rsync通过服务同步10.34 linux系统日志10.35 screen工具

- 10.32/10.33 rsync通过服务同步 - 10.34 linux系统日志 - 10.35 screen工具 - 扩展 1. Linux日志文件总管logrotate http://linux.cn/article-4126-1.html 2. xargs用法详解 http://blog.csdn.net/zhangfn2011/article/details/6776925 # 10.32 rsync通过服务来同步 上 - rsync通过服务的方式同步 - 要编辑配置文件/etc/