Polycarp's Practice(从一组数据中找出前n个最大的数的及其下标)

本文介绍了一个问题,即如何将n个不同难度的问题分配到k天内完成,以使得每天的最高难度之和达到最大。通过模拟的方式,找出最大的k个难度值,并据此规划每天解决问题的数量。

Polycarp is practicing his problem solving skill. He has a list of nn problems with difficulties a1,a2,…,ana1,a2,…,an, respectively. His plan is to practice for exactly kk 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 nn problems in exactly kk 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 kk days he will solve all the nn problems.

The profit of the jj-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the jj-th day (i.e. if he solves problems with indices from ll to rr during a day, then the profit of the day is maxl≤i≤raimaxl≤i≤rai). The total profit of his practice is the sum of the profits over all kk 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 nnproblems between kk days satisfying the conditions above in such a way, that the total profit is maximum.

For example, if n=8,k=3n=8,k=3 and a=[5,4,2,6,5,1,9,2]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][5,4,2],[6,5],[1,9,2]. Here the total profit equals 5+6+9=205+6+9=20.

Input

The first line of the input contains two integers nn and kk (1≤k≤n≤20001≤k≤n≤2000) — the number of problems and the number of days, respectively.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤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 kk positive integers t1,t2,…,tkt1,t2,…,tk (t1+t2+⋯+tkt1+t2+⋯+tk must equal nn), where tjtj means the number of problems Polycarp will solve during the jj-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

题意:一个人需要在k天解决n个问题,现在把这些问题,分成k段,表示每一天解决的问题,然后有个最大解决问题,是每一段的最大解决问题的数的和。然后在输出每一段的长度。

题解:模拟 先找出里面最大的k个数存入一个数组,从左到右遍历,如果当前问题在最大问题数组里面,就把下标记录下来,或者直接计算长度,与上一个坐标差。

#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
int n,m,a[2001],p[2001],k,s;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)scanf("%d",a+i);
    for(int j=2000; j; j--)
    {
        for(int i=1; i<=n; i++)
        {
            if(j==a[i])
            {
                p[++k]=i;
            }
        }
    }
    sort(p+1,p+1+m);
    for(int j=1; j<=m; j++)
        s+=a[p[j]];
    printf("%d\n",s);
    if(m>1)
        printf("%d ",p[1]);
    for(int j=2; j<m; j++)
        printf("%d ",p[j]-p[j-1]);
    printf("%d",n-p[m-1]);
    return 0;
}

 

 

用c++编写,使用简写变量名几个字母内: # 问题描述 排列是指由 1 到 n 的整构成的序列,其中每个字恰好出现一次。例如,[1]、[3,5,2,1,4]、[1,3,2] 是排列,而 [2,3,2]、[4,3,1]、[0] 则不是。 Polycarp 得到了一个从 1 到 n 的排列 p。然而,当 Polycarp 回到家时,他发现排列 p 按照以下规则变成了组 q: \[ q_i = \max(p_1, p_2, \ldots, p_i) \] 现在 Polycarp 想知道,原本可能呈现给他的排列中,字典序最小的排列和字典序最大的排列分别是什么。 一个长度为 n 的组 a 如果在某个位置 i(\(1 \leq i \leq n\))上, \(i-1\) 个元素与组 b 相同,且第 i 个元素比组 b 的第 i 个元素小,则称组 a 的字典序小于组 b。例如,组 \(\boldsymbol{a} = [1,3,2,3]\) 的字典序小于组 \(\boldsymbol{b} = [1,3,4,2]\)。 例如,当 \(n = 7\) 时,若 \(p = [3,2,4,1,7,5,6]\),则 \(q = [3,3,4,4,7,7,7]\)。以下排列都可能是最初的 p: - [3, 1, 4, 2, 7, 5, 6](字典序最小的排列) - [3,1,4,2,7,6,5] - [3,2,4,1,7,5,6] - [3,2,4,1,7,6,5](字典序最大的排列) 给定组 q,请找出最初可能呈现给 Polycarp 的字典序最小的排列和字典序最大的排列。 # 输入 第一行包含一个整 t(\(1 \leq t \leq 10^4\)),表示测试用例的量。接下来是 t 个测试用例。 每个测试用例的第一行包含一个整 n(\(1 \leq n \leq 2 \times 10^5\))。 每个测试用例的第二行包含 n 个整 \(q_1, q_2, \ldots, q_n\)(\(1 \leq q_i \leq n\))。 保证组 q 是由某个排列 p 按照上述规则生成的。 保证所有测试用例的 n 之和不超过 \(2 \times 10^5\)。 # 输出 对于每个测试用例,输出两行: - 第一行输出 n 个整,表示字典序最小的排列。 - 第二行输出 n 个整,表示字典序最大的排列。
07-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值