CodeForces - 940E Cashback

本文介绍了一种使用动态规划和范围最小查询(RMQ)解决最优子数组划分问题的方法,目标是最小化所有连续子数组的价值之和,其中每个子数组的价值为其元素总和减去最小值。

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

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.

Example

Input
3 5
1 2 3
Output
6
Input
12 10
1 1 10 10 10 10 10 10 9 10 10 10
Output
92
Input
7 2
2 3 6 4 5 7 1
Output
17
Input
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.

 

做法:

一道dp

设dp[i]为前i个的最小答案

那么

dp[i] = min(dp[i-1] , dp[i-c] + sum[(a[ i-c+1] ), a[i] ] - min(a[i-c+1] , a[i] ) )

 

代码:

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 #include<cstring>
 5 #define read(x) scanf("%I64d",&x)
 6 template<class T>
 7 class RMQ{
 8     public:
 9      T* a;
10      int t;
11      T **mn;
12      T maxn;
13      int *log2;
14      void SetMaxn(T *maxn){
15         this->maxn=*maxn;
16      }
17      void SetMaxn(T maxn){
18         this->maxn=maxn;
19      }
20      void Creat(T a[],int maxn){//建立一个最大为maxn的RMQ处理类 
21           int k=1,p=0;
22           log2=new int[maxn+10];
23           for(int i=0;i<=maxn;i++)
24               log2[i]=(i==0?-1:log2[i>>1]+1);
25           while(k<maxn){
26              k*=2;
27             p+=1;
28         }
29         t=p;
30         mn=new T*[maxn+10];
31         for(int i=0;i<maxn;++i){
32             mn[i] = new T[t+1];
33             mn[i][0]=a[i];
34             for(int j=1;j<=t;++j)
35                 mn[i][j]=maxn;
36         }
37          for(int j=1;j<=t;++j)
38              for(int i=0;i+(1<<j) <= maxn;++i){
39                      T sa=mn[i][j-1];
40                     T sb=mn[i+(1<<(j-1))][j-1];
41                      if(sa<sb)
42                          mn[i][j]=sa;
43                      else
44                          mn[i][j]=sb;
45              }    
46      }
47     T Getx(int ql,int qr){
48             int k=log2[qr-ql+1];
49             return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
50     }
51     T Getw(int ql,int qr){
52             --ql,--qr;
53             int k=log2[qr-ql+1];
54             return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
55     }
56 } ;
57 typedef long long LL;
58 RMQ<LL> rr;
59 LL n,c;
60 LL a[100100];
61 LL f[100100];
62 LL s[100100];
63 int main(){
64     rr.SetMaxn(1e15);
65     s[0]=0;
66     memset(f,-1LL,sizeof(f));
67     read(n);
68     read(c);
69     for(int i=1;i<=n;i++){
70         read(a[i]);
71         s[i] = s[i-1] + a[i];    
72     }
73     a[0]=1e15;
74     f[0]=0;
75     rr.Creat(a,n+1);
76     for(int i=1;i<=n;i++){
77         f[i] = f[i-1] + a[i];
78         LL zans = 1e15;
79         if(i-c>=0)
80             zans = f[i-c] + s[i] - s[i-c] - rr.Getx(i-c+1,i);
81         if (f[i]>zans)
82             f[i] = zans;
83     //    cout<<i<<" "<<f[i]<<endl;
84     }
85     printf("%I64d",f[n]);
86     return 0;
87 }

 

 

转载于:https://www.cnblogs.com/xfww/p/8468824.html

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值