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 ...
随机推荐
- English-培训6-Do you like rap?
- Linux建立虚拟ip的方法
文章来源 运维公会:Linux建立虚拟ip的方法 1.虚拟ip的介绍 虚拟IP地址(VIP) 是一个不与特定计算机或一个计算机中的网络接口卡(NIC)相连的IP地址.数据包被发送到这个VIP地址, ...
- springboot 打包发布(war包)
版本关系: 软件名称 版本号 软件名称 版本号 spring boot 2.x jdk 1.8 tomcat 9.x springboot中的pom.xml文件 打包:右键点击项目,选择如下图: 填写 ...
- JS与小程序页面生命周期
Page({ /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周 ...
- 【FRDM-K64F学习笔记】使用ARM mbed和Keil MDK下载你的第一个程序
FRDM-K64F开发平台采用MK64FN1M0VLL12微控制器.该控制器包含一个带有浮点单元的ARM Cortex-M4内核.其最高工作频率为120MHz,具有256KB的RAM.1MB闪存以及许 ...
- UML之九种图
UML说是九种图吧!其实是众说纷纭,不管有几种图,我们只要能够很好的运用这几张图就好,主要有用例图.类图.对象图.状态图.活动图.序列图.协作图.构件图和部署图,至于包图是否属于这九种图,我也理不清楚 ...
- vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin in your webpack config.
默认,webpack无法打包.vue文件,需要安装 相关的loader: cnpm i vue-loader vue-template-compiler -D 提示以下错误信息: Module Err ...
- List集合和Set集合UML图总结
1.List和Set,用RationalRose展示 2.Map
- Java出现 The server time zone value '�й���ʱ��' is unrecognized 异常
解决办法: 在配置连接数据库的URL后面加上?serverTimezone=UTC ,如下: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
- 你所不知道的printf函数
#include <stdio.h> int main(void) { int a = 4; int b = 3; int c = a / b; float d = *(float *)( ...