zoukankan      html  css  js  c++  java
  • CodeForces-1006B-Polycarp's Practice

    B. Polycarp's Practice

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    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

    Copy

    8 3
    5 4 2 6 5 1 9 2
    

    output

    Copy

    20
    3 2 3

    input

    Copy

    5 1
    1 1 1 1 1
    

    output

    Copy

    1
    5
    

    input

    Copy

    4 2
    1 2000 2000 2
    

    output

    Copy

    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][1,2000],[2000,2]. The total profit of this distribution is 2000+2000=40002000+2000=4000.

    Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

    The only programming contests Web 2.0 platform

    Server time: Jul/18/2018 00:42:04UTC+8 (d2).

    Desktop version, switch to mobile version.

    Privacy Policy

    题解:就是找前K大的几个数,输出值几个大数之间的距离;找到前K大的数的位置,然后往两侧扩展即可。

    AC代码为:

    #include<bits/stdc++.h>
    using namespace std;
    int a[2010],vis[2010],ans[2010];
    struct Node{
        int num,id;
    } node[2010];
    bool cmp(Node a,Node b)
    {
        return a.num>b.num;
    }
    bool cmp1(Node a, Node b)
    {
        return a.id<b.id;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n,k;
        cin>>n>>k;
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            node[i].num=a[i];
            node[i].id=i;
        }
        sort(node+1,node+1+n,cmp);
        int sum=0;
        for(int i=1;i<=k;i++) sum+=node[i].num,vis[node[i].id]=1;
        cout<<sum<<endl;
        int temp=0,flag1,flag2;
        for(int i=1;i<=n;i++)
        {
            if(vis[i])
            {
                flag1=i-1;flag2=i+1;
                while(!vis[flag1] && flag1>0&&flag1<=n) vis[flag1]=1,flag1--;
                while(!vis[flag2] && flag2>0&&flag2<=n) vis[flag2]=1,flag2++;
                ans[temp++]=flag2-flag1-1; 
                i=flag2-1;
            }
        }
        for(int i=0;i<temp;i++) i==temp-1 ? cout<<ans[i]<<endl : cout<<ans[i]<<" ";
        return 0;
    }

  • 相关阅读:
    安卓项目文件夹解析
    网页嵌入腾讯视频方式
    Thinkphp3.23 连接MSSQL方法
    .NET 出现参数化查询 需要参数但未提供该参数的错误
    .NET 原理之 ViewState
    VS2012创建WebForm项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
    及时返回,业务逻辑异步实现
    linux常用命令
    ES报错引发的问题
    mall项目遇到的异常
  • 原文地址:https://www.cnblogs.com/csushl/p/9386513.html
Copyright © 2011-2022 走看看