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。
53 0
LeetCode 383. Ransom Note
PAT (Advanced Level) Practice 1044 Shopping in Mars (前缀和 二分)
PAT (Advanced Level) Practice 1044 Shopping in Mars (前缀和 二分)
75 0
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
91 0
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
99 0
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
97 0
PAT (Advanced Level) Practice - 1078 Hashing(25 分)
PAT (Advanced Level) Practice - 1078 Hashing(25 分)
81 0
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
87 0
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
123 0
|
Shell
Solving environment: failed with initial frozen solve. Retrying with flexible solve的解决方法
Solving environment: failed with initial frozen solve. Retrying with flexible solve的解决方法
11832 0
Solving environment: failed with initial frozen solve. Retrying with flexible solve的解决方法
LeetCode之Ransom Note
LeetCode之Ransom Note
98 0