玲珑杯 1014 - Absolute Defeat

本文介绍了一个关于数组操作的算法问题,目标是最小化操作次数使数组中所有子序列的最小值不低于给定阈值。通过从左到右扫描数组并调整不足的子序列来解决该问题。
1014 - Absolute Defeat

Time Limit:2s Memory Limit:64MByte

Submissions:257Solved:73

DESCRIPTION
Eric has an array of integers a1,a2,...,ana1,a2,...,an. Every time, he can choose a contiguous subsequence of length kk and increase every integer in the contiguous subsequence by 11.He wants the minimum value of the array is at least mm. Help him find the minimum number of operations needed.
INPUT
There are multiple test cases. The first line of input contains an integerTT, indicating the number of test cases. For each test case:The first line contains three integersnn,mm and kk(1n105,1kn,1m104)(1≤n≤105,1≤k≤n,1≤m≤104).The second line contains nn integers a1,a2,...,ana1,a2,...,an(1ai104)(1≤ai≤104).
OUTPUT
For each test case, output an integer denoting the minimum number of operations needed.
SAMPLE INPUT
32 2 21 15 1 41 2 3 4 54 10 31 2 3 4
SAMPLE OUTPUT

1015


【解题方法】思维。从左往右枚举每个元素, 如果不够的话, 直接把这个元素起始的长度为m的序列都改一下就好了.

【AC 代码】

//
//Created by just_sort 2016/9/25 21:03
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int a[1000010];
int main()
{
    int T,n,k,m;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        int ans = 0;
        int temp;
        for(int i=1; i<=n; i++){
            if(a[i]<m){
                temp = m - a[i];
                ans += temp;
                for(int j=i; j<i+k; j++){
                    a[j] += temp;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值