CF 1006B Polycarp's Practice【贪心】
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a2,…,an, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is maxl≤i≤rai). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n=8,k=3 and a=[5,4,2,6,5,1,9,2], one of the possible distributions with maximum total profit is: [5,4,2],[6,5],[1,9,2]. Here the total profit equals 5+6+9=20.
Input
The first line of the input contains two integers n and k (1≤k≤n≤2000) — the number of problems and the number of days, respectively.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤2000) — difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t1,t2,…,tk (t1+t2+⋯+tk must equal n), where tj means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1,2000],[2000,2]. The total profit of this distribution is 2000+2000=4000.
【题意】:给定一个数字n和m和大小为n的数组,将数组分为m个区间,要求区间最大值之和最大值以及分区大小(不唯一)。
【分析】:用结构体记录数值和位置,由大到小排序得到前k个数值之和就是最大值。难点是求分区大小,可以建立一个新数组id记录前k大数的位置,注意位置需要从小到大排序。比如第一个样例就是0 3 6,那么分区为3 3 2——>3-0/6-3/8-6
【代码】:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n,m;
struct node
{
int num,pos;
}a[N];
vector<int> v;
int id[N];
bool cmp(node a,node b)
{
return a.num > b.num;
}
int main()
{
scanf("%d%d",&n,&m);
int ans = 0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i].num);
a[i].pos = i;
}
sort(a,a+n,cmp);
for(int i=0; i<m; i++)
{
id[i] = a[i].pos;
ans += a[i].num;
}
cout<<ans<<endl;
id[m]=n;
sort(id,id+m+1);
printf("%d ",id[1]);
for(int i=1; i<m-1; i++)
{
printf("%d ",id[i+1]-id[i]);
}
if(m!=1) printf("%d\n",id[m]-id[m-1]);
}
CF 1006B Polycarp's Practice【贪心】的更多相关文章
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
- CF 435B Pasha Maximizes(贪心)
题目链接: [传送门][1] Pasha Maximizes time limit per test:1 second memory limit per test:256 megabytes ...
- [CF #288-C] Anya and Ghosts (贪心)
题目链接:http://codeforces.com/contest/508/problem/C 题目大意:给你三个数,m,t,r,代表晚上有m个幽灵,我有无限支蜡烛,每支蜡烛能够亮t秒,房间需要r支 ...
- cf 605A Sorting Railway Cars 贪心 简单题
其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...
- CF 1141C Polycarp Restores Permutation
Description An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each nu ...
- CF D. Walking Between Houses (贪心)
题意: 现在有n个房子排成一列,编号为1~n,起初你在第1个房子里,现在你要进行k次移动,每次移动一都可以从一个房子i移动到另外一个其他的房子j里(i != j),移动的距离为|j - i|.问你进过 ...
- CF:322D - Ciel and Duel 贪心 或者 DP 我用的贪心 。。难道sort跟qsort是不一样的么?
D. Ciel and Duel time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF 360E Levko and Game——贪心
题目:http://codeforces.com/contest/360/problem/E 官方题解与证明:http://codeforces.com/blog/entry/9529 一条可以调整的 ...
- nyoj 1216——整理图书 CF 229D—— Towers——————【dp+贪心】
整理图书 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 小明是图书鹳狸猿,他有很多很多的书堆在了一起摆在了架子上,每摞书是横着放的,而且每摞书是订好的 是一个整体, ...
随机推荐
- P3457 [POI2007]POW-The Flood
题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是否是该市的一个组成部 ...
- POJ——2449 Remmarguts' Date
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- [洛谷P3935]Calculating
题目大意:设把$x$分解质因数的结果为$x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n}$,令$f(x)=(k_1+1)(k_2+1)\cdots (k_n+1)$,求$\su ...
- 活泼的CSS 3动态气泡按钮制作
这一次,我们正在创造一个有用的设置与对CSS3的多重背景和动画的力量动画按钮.通过此按钮包,您可以很容易地变成一个动画按钮,在您的网页上的任何链接只是指定一个类名.没有必要JavaScript.四色主 ...
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...
- Django请求原理
总结一下: 1. 进来的请求转入/hello/. 2. Django通过在ROOT_URLCONF配置来决定根URLconf. 3. Django在URLconf中的所有URL模式中,查找第一个匹配/ ...
- TCP ------ TCP四次挥手(断开连接)及断开过程
1.正常情况下,调用close(),产生的其中一个效果就是发送FIN,只有双方都调用close(),才会出现正常的四次挥手. 2.如果是服务器,发起四次挥手是在关闭accept()返回的套接字,而不是 ...
- MySQL 8.0.11 中使用 grant ... identified by 时 error 1064 near 'identified by '密码'' at line 1
(1)问题: 当使用 grant 权限列表 on 数据库 to '用户名'@'访问主机' identified by '密码'; 时会出现"......near 'identifie ...
- bzoj 1026 DP,数位统计
2013-11-20 08:11 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1026 首先我们用w[i,j]表示最高位是第i位,且是j的 ...
- C# 文件类的操作---获取
如何获取指定目录包含的文件和子目录 . DirectoryInfo.GetFiles():获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找: . DirectoryIn ...