CF1006B Polycarp's Practice 题解
Content
给定一个长度为 \(n\) 的数列,试将其分成 \(k\) 段,使得每一段中的最大值的和最大。
数据范围:\(1\leqslant k,n,a_i\leqslant 2000\)。
Solution
我们不难发现,最优的方案其实就是将前 \(k\) 大的数各自单独放在一段里面,所以我们排序得到前 \(k\) 大的数,再找到他们的位置。那么如何记录每一段里面的其他数呢?我的方案是,将前 \(k-1\) 大的数作为每一段的最后一个数存储,剩下的那个数所在的段就是没有被前面的 \(k-1\) 个段占的部分,这下就可以轻松地解决问题了。
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int a[2007], sum, n, k, ids[2007];
struct node {
int val, id;
bool operator < (const node& ou) const {return val > ou.val;}
}a1[2007];
int main() {
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
a1[i].val = a[i], a1[i].id = i;
}
sort(a1 + 1, a1 + n + 1);
for(int i = 1; i <= k; ++i) ids[i] = a1[i].id, sum += a1[i].val;
sort(ids + 1, ids + k + 1);
printf("%d\n", sum);
int lastans = 0;
for(int i = 1; i < k; ++i) {printf("%d ", ids[i] - ids[i - 1]); lastans += ids[i] - ids[i - 1];}
printf("%d", n - lastans);
return 0;
}
CF1006B Polycarp's Practice 题解的更多相关文章
- CF 1006B Polycarp's Practice【贪心】
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a ...
- CF1141C Polycarp Restores Permutation 题解
Content 给定一个长度为 \(n-1\) 的序列 \(q\),问你是否能找到一个 \(1\sim n\) 的排列 \(p\),使得 \(\forall i\in[1,n)\),\(q_i=p_{ ...
- Codeforces Round #498 (Div. 3) 简要题解
[比赛链接] https://codeforces.com/contest/1006 [题解] Problem A. Adjacent Replacements [算法] 将序列中的所有 ...
- CodeForces-1006B-Polycarp's Practice
B. Polycarp's Practice time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- HDU-1017
A Mathematical Curiosity Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- Codeforces Div3 #498 A-F
. A. Adjacent Replacement ...
- VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]
传送门 A. Reposts time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 校省选赛第一场C题解Practice
比赛时间只有两个小时,我没有选做这题,因为当时看样例也看不懂,比较烦恼. 后来发现,该题对输入输出要求很低.远远没有昨天我在做的A题的麻烦,赛后认真看了一下就明白了,写了一下,一次就AC了,没问题,真 ...
- CF1277A. Happy Birthday, Polycarp! 题解 枚举/数位DP
题目链接:http://codeforces.com/contest/1277/problem/A 题目大意: 求区间 \([1,n]\) 范围内有多少只包含一个数字的数. 比如:\(1,77,777 ...
随机推荐
- [NOI2020] 制作菜品
看懂题目是生产第一要素. 考虑\(m = n - 1\)则必定有解.我们每次选择最小的和最大的拼在一起即可. 当\(m\)大于\(n\),那么我们只要每次选择最大的给他消掉即可. \(m = n - ...
- ABC201题解
因为学校的某些操作. 让最近两天的\(Atcoder\)都能打了,挺开心的. 想上次\(ABC\)看错题意,失败退场. ------------------------------ \(A\) 直接手 ...
- Congratulations, FYMS-OIers!
Fuzhou Yan'an Middle School Online Judge 又一次上线啦! 真的是一波三折,主要功劳必须得属于精通网页编排.ubuntu 下如何使用 rm -rf 语句但是又能够 ...
- 【WEGO】GO注释可视化
导入数据 BGI开发的一款web工具,用于可视化GO注释结果.自己平时不用,但要介绍给别人,简单记录下要点,避免每次授课前自己忘了又要摸索. 地址:http://wego.genomics.org.c ...
- os.path.join()函数
连接两个或更多的路径名组件 import os p1 = '/date' p2 = 'mage' p3 = 'img' all = os.path.join(p1,p2,p3) print(all) ...
- 重测序(RADseq)做群体遗传分析套路
实验材料 构建的群体,或自然群体,如各地方品种. RAD文库构建 提取DNA后,构建文库,简要步骤如下: ① 限制性内切酶TaqI酶切: ② 连接P1接头: ③ DNA随机打断片断化: ④ 目的片段回 ...
- 用Rsync实现windows下同步linux服务器的数据
一:环境 1.服务端:Red Hat Enterprise Linux Server release 6.4 (Santiago) 2.客户端:windows7旗舰版64位 3.同步对象:测试数据 4 ...
- MySQL深层理解,执行流程
MySQL是一个关系型数据库,关联的数据保存在不同的表中,增加了数据操作的灵活性. 执行流程 MySQL是一个单进程服务,每一个请求用线程来响应, 流程: 1,客户请求,服务器开辟一个线程响应用户. ...
- kubernetes部署Docker私有仓库Registry
在后面的部署过程中,有很多的docker镜像文件,由于kubernetes是使用国外的镜像,可能会出现下载很慢或者下载不下来的情况,我们先搭建一个简单的镜像服务器,我们将需要的镜像下载回来,放到我们自 ...
- A Child's History of England.47
CHAPTER 13 ENGLAND UNDER RICHARD THE FIRST, CALLED THE LION-HEART In the year of our Lord one thousa ...