Codeforces 940E-Cashback

本文介绍了一个关于序列划分的问题,目标是最小化特定条件下的子序列贡献总和。通过动态规划和线段树来高效地解决这个问题,实现了对给定序列的最佳划分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cashback
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount.

You are given an array a of length n and an integer c.

The value of some array b of length k is the sum of its elements except for the  smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.

Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.

Input

The first line contains integers n and c (1 ≤ n, c ≤ 100 000).

The second line contains n integers ai (1 ≤ ai ≤ 109) — elements of a.

Output

Output a single integer  — the smallest possible sum of values of these subarrays of some partition of a.

Examples
input
Copy
3 5
1 2 3
output
6
input
Copy
12 10
1 1 10 10 10 10 10 10 9 10 10 10
output
92
input
Copy
7 2
2 3 6 4 5 7 1
output
17
input
Copy
8 4
1 3 4 5 5 3 4 1
output
23
Note

In the first example any partition yields 6 as the sum.

In the second example one of the optimal partitions is [1, 1], [10, 10, 10, 10, 10, 10, 9, 10, 10, 10] with the values 2 and 90 respectively.

In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.

In the fourth example one of the optimal partitions is [1], [3, 4, 5, 5, 3, 4], [1] with the values 1, 21 and 1 respectively.


题意:给一个长度为n的序列ai,给出一个整数c定义序列中一段长度为k的区间的贡献为区间和减去前⌊k/c⌋小数的和,现在要给序列ai做一个划分,使得贡献的总和最小。

解题思路:首先可以证明出最优方案中一定存在所有划分的块的元素量都不大于c。可以考虑dp,dp[i]表示前i个元素可以被砍掉的最大和,则只需考虑第i个元素是否被取到即可,被取到时,dp[i] = dp[i - c] + min(a[i - c + 1...i]);没被取到时,dp[i] = dp[i - 1]。其中求最小值可以用线段树维护。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long     
const int INF = 0x3f3f3f3f;

int mi[400015], n, c;
LL sum;
LL dp[100015];

void build(int k, int l, int r)
{
	if (l == r)
	{
		scanf("%d", &mi[k]);
		sum += mi[k];
		return;
	}
	int mid = l + r >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
	mi[k] = min(mi[k << 1], mi[k << 1 | 1]);
}

int query(int k, int l, int r, int ll, int rr)
{
	if (ll <= l && r <= rr) return mi[k];
	int mid = l + r >> 1;
	int ans = INF;
	if (ll <= mid) ans = min(ans, query(k << 1, l, mid, ll, rr));
	if (mid < rr) ans = min(ans, query(k << 1 | 1, mid + 1, r, ll, rr));
	return ans;
}

int main()
{
	while (~scanf("%d%d", &n, &c))
	{
		sum = 0;
		build(1, 1, n);
		memset(dp, 0, sizeof dp);
		for (int i = c; i <= n; i++)
		{
			int tmp = query(1, 1, n, i - c + 1, i);
			dp[i] = max(dp[i - 1], dp[i - c] + tmp);
		}
		printf("%lld", sum - dp[n]);
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值