codeforces940e(dp)

E. 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数组a和整数c。把a分成若干连续段,每一段对应的值等于其对应sum减去段长k除以c向下取整各的最小几个数,求所有段值的和。

思路:贪心,必定是把a分成若干长为c和1的段。线段树查询区间最小值。


线段树+dp代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#define L(x) (x << 1)//左儿子
#define R(x) (x << 1 | 1)//右儿子
#define sz(x) (tree[x].r - tree[x].l + 1)//区间大小
using namespace std;
typedef long long ll;
const int MAXN = 100005;
const int inf=0x3f3f3f3f;
ll n,m,a,b,c,num[MAXN];
ll sum[MAXN];
ll dp[MAXN];
// l == 左;r == 右;p == now;
struct tree{
    int l,r;//修改求和
    int min;//最值标记
}tree[MAXN << 2];

//标记下放们 p == now
void update(int p){
    tree[p].min = min(tree[L(p)].min,tree[R(p)].min);
}
//简单的建立
void build(int l,int r,int p){
    tree[p].l = l,tree[p].r = r;
    if(l == r){
        tree[p].min=num[l];
        return;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    build(l,mid,L(p));  build(mid + 1,r,R(p));
    update(p);
}

//区间最小值
int ask_min(int l,int r,int p){
    if(l <= tree[p].l && tree[p].r <= r)
        return tree[p].min;
    int ans = inf,mid = (tree[p].l + tree[p].r) >> 1;
    if(l <= mid)    ans = min(ans,ask_min(l,r,L(p)));
    if(mid < r)     ans = min(ans,ask_min(l,r,R(p)));
    update(p);      return ans;
}

int main(){
	memset(dp,0,sizeof(dp));
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    scanf("%d",&n);
    scanf("%d",&c);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d",&num[i]);
        dp[i]=sum[i]=sum[i-1]+num[i];
    }
    build(1,n,1);   
	for(int i=c;i<=n;i++)
	{
		dp[i]=min(dp[i-1]+num[i],dp[i-c]+sum[i]-sum[i-c]-ask_min(i-c+1,i,1));
	}
	printf("%I64d\n",dp[n]);
    return 0;
}


multiset + dp代码:

#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
const int maxn = 100007;
int a[maxn],n,c;
ll dp[maxn],sum[maxn];
multiset<int> st;
int main(){
    scanf("%d%d",&n,&c);
    for(int i = 1;i <= n;++i){
        scanf("%d",&a[i]);
        dp[i] = sum[i] = sum[i-1] + a[i];
    }
    for(int i = 1;i < c;++i)   //把前c个元素加入multiset
        st.insert(a[i]);
    for(int i = c;i <= n;++i){
        st.insert(a[i]);     //加入新元素
        dp[i] = min(dp[i-1] + a[i],dp[i-c] + sum[i] - sum[i-c] - *st.begin());   //两种分法比较
        st.erase(st.find(a[i-c+1]));   //去除旧元素
    } 
    printf("%lld\n",dp[n]);
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值