题目链接

题意

给出一个长度为\(n\)的序列\(a\),要求分为恰好\(K\)段。第\(i\)个点的贡献是\(a_i \times f(i)\),\(f(x)\)表示x所属的是第几段。

思路

非常巧妙的一个思路。

先让每个元素都选K遍。然后不断的删除。

具体做法就是,先求一遍前缀和。然后找出前缀和最小的\(K-1\)个前缀,将其从答案中减去。初始答案为所有元素和\(\times K\)

这样被减j遍的元素就位于第\(K-j\)段中。因为是前缀和。所以前边点被减的次数一定大于等于后边。然后就符合题意了。

代码

/*
* @Author: wxyww
* @Date: 2019-06-06 07:50:48
* @Last Modified time: 2019-06-06 07:56:06
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 300000 + 100;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
ll a[N];
int main() {
int n = read(),K = read();
for(int i = 1;i <= n;++i) a[i] = read() + a[i - 1];
ll ans = a[n] * K;
sort(a + 1,a + n);
for(int i = 1;i <= K - 1;++i) ans -= a[i];
cout<<ans;
return 0;
}

CF1175D 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. cf754 A. Lesha and array splitting

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

  8. Codeforces Round #390 (Div. 2) A. Lesha and array splitting

    http://codeforces.com/contest/754/problem/A 题意: 给出一串序列,现在要把这串序列分成多个序列,使得每一个序列的sum都不为0. 思路: 先统计一下不为0的 ...

  9. Codeforce 1175 D. Array Splitting

    新鲜热乎的题 Codeforce 1175 D. 题意:给出一个长度为$n$的序列$a$,你需要把它划分为$k$段,每一个元素都需要刚好在其中一段中.分好之后,要计算$\sum_{i=1}^{n} ( ...

随机推荐

  1. 解决node fs.writeFile 生成csv 文件乱码问题

    解决node fs.writeFile 生成csv 文件乱码问题: fs.writeFile('xxx.csv', '\ufeff' + 要传入的数据, {encoding: 'utf8'}); \u ...

  2. 03Shell条件测试

    条件测试 Shell 条件测试 格式 1: test 条件表达式 格式 2: [ 条件表达式 ] 格式 3: [[ 条件表达式 ]] 具体参数说明可以通过 man test 进行查看 文件测试 [ 操 ...

  3. ImportError: cannot import name 'render_to_response' 解决方法

    前几天 Django 官方推出了 3.0 框架,项目在 K8S 内部署启动的时候,报了这个错:ImportError: cannot import name 'render_to_response' ...

  4. [Node.js] TypeScript 实现 sleep 函数

    看过不少网友的文章, 有各种方法, 但我想要的是一个能线性执行的sleep函数. /** * 等待指定的时间 * @param ms */ static async sleep(ms: number) ...

  5. 2019-9-18-WPF-如何调试-binding

    原文:2019-9-18-WPF-如何调试-binding title author date CreateTime categories WPF 如何调试 binding lindexi 2019- ...

  6. jakarta-oro-2.0.8.jar-----------JAVA FTP相关

    资源不好找,找到了就和大家分享一下! 链接:https://share.weiyun.com/51kBB0y 密码:2hcxcu

  7. PIE SDK水深提取算法

    1.算法功能简介 水深提取算法就是根据输入的水位设为d,dem设为h 这两个数据做一个差值运算,则水深计算公式为d-h;本示例中的是基于洞庭湖提取的水体矢量文件的范围来计算dem和水位25米的差值. ...

  8. List的Clear方法与RemoveAll方法用法小结

    转自:https://blog.csdn.net/yl2isoft/article/details/17059093 结果分析 执行List的Clear方法和RemoveAll方法,List将清除指定 ...

  9. PDO封装增删改查

    <?phpclass db{ public $table=null; public $pdo; public $where=null; //where 条件 public $field=null ...

  10. element-ui Rate组件源码分析整理笔记(十三)

    Rate组件源码比较简单,有添加部分注释 main.vue <template> <!--valuenow当前的评分 valuetext当前显示的文本--> <div c ...