UESTC 1817 Complete Building the Houses 贪心

简介:

同是比赛时候的一道水题,比赛的时候以为满层的就不能继续建了,没想到可以忽视它……

搞清题意后,唯一要注意的一点就是ans是long long的


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int a[1000001];
long long ans=0;
int main()
{
    int now;
    int T,C=0;
    int n,m,i,t;
    scanf("%d",&T);
    while(T--)
    {
        ans=now=0;
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%d",&t);
            if(now>=t)
                a[i]=0;
            else
            {
                a[i]=t-now;
                ans+=a[i];
                now=t;
            }
            if(i+1-m>=0)//超过m长度
                now-=a[i+1-m];
        }
        printf("Case #%d: %lld\n",++C,ans);
    }
}


目录
相关文章
LeetCode 383. Ransom Note
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。
68 0
LeetCode 383. Ransom Note
|
机器学习/深度学习 计算机视觉
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
PAT (Advanced Level) Practice 1044 Shopping in Mars (前缀和 二分)
PAT (Advanced Level) Practice 1044 Shopping in Mars (前缀和 二分)
96 0
|
C++
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
124 0
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
109 0
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
118 0
|
Shell
Solving environment: failed with initial frozen solve. Retrying with flexible solve的解决方法
Solving environment: failed with initial frozen solve. Retrying with flexible solve的解决方法
13047 0
Solving environment: failed with initial frozen solve. Retrying with flexible solve的解决方法
HDOJ 1339 A Simple Task(简单数学题,暴力)
HDOJ 1339 A Simple Task(简单数学题,暴力)
113 0
LeetCode之Ransom Note
LeetCode之Ransom Note
114 0