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 ...
随机推荐
- CVE-2019-11517 CSRF in Wampserver 3.1.4-3.1.8
https://www.cnblogs.com/iAmSoScArEd/ Affected product:WampServer 3.1.4-3.1.8 Offiical description:&q ...
- Traceback (most recent call last): File "../zubax_chibios/tools/make_boot_descriptor.py", line 251
出现如下错误: Traceback (most recent call last): File "../zubax_chibios/tools/make_boot_descriptor.py ...
- pip3升级问题
输入命令sudo pip3 install --upgrade pip 升级完成之后执行pip命令会报错,错误信息如下: File "/usr/bin/pip3", line 9, ...
- 【ogg三】日常运维篇:清理归档日志,ogg进程注册服务,定期备份数据库
清理归档日志 ogg使用需要开启归档日志,归档日志会随着时间的推移逐渐增多,占满空间,导致应用无法正常运行. 如果归档日志满了会报错 ORA-00257:archiver error解决办法 检查fl ...
- 缓存数据库memcache、redis原理对比
一.问题: 数据库表数据量极大(千万条),要求让服务器更加快速地响应用户的需求. 二.解决方案: 1.通过高速服务器Cache缓存数据库数据 2.内存数据库 (这里 ...
- Flutter——Switch组件(开关组件)
Switch组件常用的属性: 属性 描述 value 单选的值 onChanged 改变时触发 activeColor 选中的颜色.背景颜色 import 'package:flutter/mater ...
- C++——虚函数表解析
转自:https://blog.csdn.net/haoel/article/details/1948051 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型指针指 ...
- 前端笔记-js
js在html中的位置 HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 < ...
- UML之关系
学习UML我们首先要掌握他们的关系,UML关系可以分为四类,主要有关联.依赖.泛化和实现. 下面我们就一一来详细说明这几种关系. 关联 表示两个类或类与接口之间强烈的依赖关系,关联用直线表示.当然我们 ...
- python_面向对象——多态
1.同一接口,多种形态 class Document: def __init__(self,name): self.name = name def show(self): # 异常处理:提示子类必须把 ...
