CodeForces - 1175D Array Splitting(数组划分+后缀和+贪心)
You are given an array a1,a2,…,ana1,a2,…,an and an integer kk.
You are asked to divide this array into kk non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let f(i)f(i) be the index of subarray the ii-th element belongs to. Subarrays are numbered from left to right and from 11 to kk.
Let the cost of division be equal to ∑i=1n(ai⋅f(i))∑i=1n(ai⋅f(i)). For example, if a=[1,−2,−3,4,−5,6,−7]a=[1,−2,−3,4,−5,6,−7] and we divide it into 33 subbarays in the following way: [1,−2,−3],[4,−5],[6,−7][1,−2,−3],[4,−5],[6,−7], then the cost of division is equal to 1⋅1−2⋅1−3⋅1+4⋅2−5⋅2+6⋅3−7⋅3=−91⋅1−2⋅1−3⋅1+4⋅2−5⋅2+6⋅3−7⋅3=−9.
Calculate the maximum cost you can obtain by dividing the array aa into kk non-empty consecutive subarrays.
Input
The first line contains two integers nn and kk (1≤k≤n≤3⋅1051≤k≤n≤3⋅105).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (|ai|≤106|ai|≤106).
Output
Print the maximum cost you can obtain by dividing the array aa into kk nonempty consecutive subarrays.
Examples
5 2
-1 -2 5 -4 8
15
7 6
-3 0 -1 -2 -2 -4 -1
-45
4 1
3 -1 6 0
8 题意:
给定一个长度为n的数组,将其划分为k份。问怎样划分使得各份【i(第i份)*sum(i份内部和)】相加的和最大。 思路:
利用后缀和思想,用差(左减右)的形式表示连续的区间和。
根据以下公式(裂项求和)推导出结果,为k个后缀和相加。
若使答案最大,只需找出最大的k个后缀和,贪心即可。
注意:S(p1)必须为第一项的后缀和,因为要保证覆盖所有的数组元素,剩余k-1个从最大开始找。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; ll a[],suf[]; int main()
{
int t,n,k,i,j;
scanf("%d%d",&n,&k);
for(i=;i<=n;i++){
scanf("%I64d",&a[i]);
}
for(i=n;i>=;i--){
suf[i]=suf[i+]+a[i];
}
sort(suf+,suf+n+);
ll ans=suf[];
for(i=n;i>n-(k-);i--){
ans+=suf[i];
}
printf("%I64d\n",ans);
return ;
}
CodeForces - 1175D Array Splitting(数组划分+后缀和+贪心)的更多相关文章
- Codeforces 754A Lesha and array splitting(简单贪心)
A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- 基于快速排序的数组划分:2组 3组 K组(sort color)大小写排序 · Partition Array
2组: [抄题]: 给出一个整数数组 nums 和一个整数 k.划分数组(即移动数组 nums 中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中 ...
- 将数组划分成连续子序列 Split Array into Consecutive Subsequences
2018-08-04 20:47:43 问题描述: 问题描述: 本题需要的是将一个数组划分成子序列,保证每个子序列是连续的,并且长度要大于等于3. 解题思路是使用贪心算法,首先对数组中的数字进行计数, ...
- Codeforces 754A Lesha and array splitting (搜索)
题目链接 Lesha and array splitting 设s[i][j]为序列i到j的和,当s[i][j]≠0时,即可从i跳到j+1.目标为从1跳到n+1,所以按照题意暴力即可. #includ ...
- Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题
C. Array Splitting You are given a sorted array
- [TJOI2015]弦论(后缀数组or后缀自动机)
解法一:后缀数组 听说后缀数组解第k小本质不同的子串是一个经典问题. 把后缀排好序后第i个串的本质不同的串的贡献就是\(n-sa[i]+1-LCP(i,i-1)\)然后我们累加这个贡献,看到哪一个串的 ...
- golang之 Array(数组)
目录 一.Array(数组) 二.数组的定义 1. 基本语法 三.数组的初始化 1. 方式一 2. 方式二 3. 方式三 四.数组的遍历 1. 方式一:for循环遍历 2. 方式二:for range ...
- vector以及array和数组
//比较数组.vector.array #include <iostream> #include <vector> #include <array> #includ ...
随机推荐
- 上传图片,图片过大压缩处理以及解决自拍时会出现图片横屏的bug修复、长按保存图片
js部分:module.exports = { resize: function (file, callback, options) { //配置 options = Object.assign({ ...
- day01-02
- 记一次有趣的tp5代码执行
0x00 前言 朋友之前给了个站,拿了很久终于拿下,简单记录一下. 0x01 基础信息 漏洞点:tp 5 method 代码执行,payload如下 POST /?s=captcha _method= ...
- iOS音频学习笔记一:常见音频封装格式及编码格式
(1) pcm格式 pcm是经过话筒录音后直接得到的未经压缩的数据流 数据大小=采样频率*采样位数*声道*秒数/8 采样频率一般是22k或者44k,位数一般是8位或者16位,声道一 ...
- stm32 SD卡
容量等级 SD容量有8MB.16MB.32MB.64MB.128MB.256MB.512MB.1GB.2GB SDHC容量有2GB .4GB.8GB.16GB.32GB SDXC容量有32GB.48G ...
- for循环的耗时问题
结论——用变量来缓存数组长度,效率会更高
- py-2 python介绍与安装
一.python介绍 1.python背景 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Py ...
- zabbix server for Centos 6.3
1.安装LNMP 参照http://lnmp.org/install.html 2.安装zabbix service 2.1下载zabbix,并解压 wget http://nchc.dl.sourc ...
- 【转载】Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录?
Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /usr:系统级的目录,可以理解为C:/Windows/ /usr/lib:理解为C:/Windows/System32. ...
- 18计科学期总成绩明细 && 小黄衫团队预选
1.个人所有作业+团队作业总得分排名 2.千帆竞发图 3.各团队每次作业得分汇总 根据团队成绩排名,众志陈成队得分最高,预选为小黄衫团队. 对团队成绩有异议的团队,请在成绩预选发布后的两天内私信助教解 ...
