新鲜热乎的题 Codeforce 1175 D

题意:给出一个长度为$n$的序列$a$,你需要把它划分为$k$段,每一个元素都需要刚好在其中一段中。分好之后,要计算$\sum_{i=1}^{n} (a_i \centerdot f(i))$,使这个值最大。其中$f(i)$代表第$i$个元素被分在第几段中。简单来说,就是每个元素被分在第几段中就需要乘上几,使序列的和最大。

假设$sum$是$a$的前缀和,$k$个分割点分别为$l_j$,将序列分割为$[1, l_1],[l_1 + 1, l_2]......[l_{k-1} + 1, l_k]$,那么第$j$段对总和的贡献是$(sum_{l_j} - sum_{l_{j-1}}) * j$。

由此,总和应该为$sum_{l_1} + (sum_{l_2} - sum_{l_1}) * 2 + (sum_{l_3} - sum_{l_2}) * 3+……+(sum_{l_k} - sum_{l_{k-1}}) * k$,很显然,相邻两项之间存在可以抵消的部分,同时$l_k$一定为$n$。将其化简:$sum_n \centerdot k - sum_{l_1} - sum_{l_2} -……-sum_{l_{k-1}}$,那么我们只需要在前$n-1$项的前缀和中找出最小的$k-1$个就可以得到最大的总和了。

int n, k;
int a;
ll sum[maxn];
int main() {
scanf("%d %d", &n, &k);
priority_queue<ll, vector<ll>, greater<> > q;
for(int i = ; i <= n; ++i) {
scanf("%d", &a);
sum[i] = sum[i - ] + a;
if(i != n)
q.push(sum[i]);
}
ll ans = sum[n] * k;
for(int i = ; i <= k - ; ++i) {//找出最小的k-1个前缀和
ans -= q.top();
q.pop();
}
printf("%lld\n", ans);
}

Codeforce 1175 D. Array Splitting的更多相关文章

  1. Codeforces 754A Lesha and array splitting(简单贪心)

    A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...

  2. 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 ...

  3. Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题

    C. Array Splitting You are given a sorted array

  4. 【codeforces 754A】Lesha and array splitting

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 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 in ...

  6. D. Array Splitting(后缀数组)

    You are given an array

  7. Codeforce 1155D Beautiful Array(DP)

    D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...

  8. codeforce 121E - Lucky Array

    10^4以内只由4和7构成的数字只有31种,那么做法就很简单了,求出每个数字与其最接近的幸运数的差值,然后建立线段树,线段树维护区间最小值和最小值个数,如果操作过程中最小值<0,那么就去对差值进 ...

  9. cf754 A. Lesha and array splitting

    应该是做麻烦了,一开始还没A(幸好上一次比赛水惨了) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL lon ...

随机推荐

  1. git常用相关操作

    // 账号密码克隆远程项目 git clone http://账号:密码@项目地址 // 查看当前状态 git status // 查看修改内容 git diff // 添加并提交 git add . ...

  2. SQLServer死锁查询

    --查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys ...

  3. MySQL 保存镜像实战操作( 拷贝方法 )

    查看数据保存的位置 docker inspect --format='{{.Mounts}}' mxg_mysql 容器路径为:`/var/lib/mysql` ,宿主机数据保存在: /var/lib ...

  4. 5-基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台

    基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台 一.板卡概述      本板卡系自主研发,基于CPCI 6U架构,符合CPCI2.0标准.采用 DSP TMS320C66 ...

  5. LNMP集群架构篇

    一.LNMP介绍 1.使前端web服务和后端存储服务进行串联 2.主要实现处理php动态请求 工作原理: L:linux  N:nginx  M:mysql   P:php 二.lnmp部署 我的环境 ...

  6. Redis分布式锁【实战】

    概述 目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个分布式系统都无法同时满足一致性(Consistency).可 ...

  7. Sass @debug

    @debug 在 Sass 中是用来调试的,当你的在 Sass 的源码中使用了 @debug 指令之后,Sass 代码在编译出错时,在命令终端会输出你设置的提示 Bug: @debug 10em + ...

  8. centos上部署flask项目之环境配置-MySQL的安装

    1.添加mysql 的yum源 wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'    rpm ...

  9. SpringBoot整合MyBatis-Plus实现快速业务功能开发

    概览:使用MybatisPlus和它的代码生成整合SpringBoot可以实现快速的业务功能开发,具体步骤如下 一.添加依赖 <dependency> <groupId>org ...

  10. 对Promise的研究2

    3.Promise.prototype.then() Promise 实例具有then方法,也就是说,then方法是定义在原型对象Promise.prototype上的.它的作用是为 Promise ...