zoukankan      html  css  js  c++  java
  • Codeforces Round #498 (Div. 3) B. 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.

    emm阅读理解

    题目要求

    将数组分为N段

    每段的价值是该段中最大元素的值

    1 2 6 5 段的价值是6

    求这N段最大价值和

    结构体存每个元素的值和位置

    找到前N大的元素 加和

    按前N大元素的位置进行分段

    注意边界

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int MAXN = 2e4 + 10;
    
    struct num
    {
    	int val;
    	int pos;
    }arr[MAXN];
    
    bool cmp(num a, num b)
    {
    	return a.val > b.val;
    }
    
    bool cmp1(num a, num b)
    {
    	return a.pos < b.pos;
    }
    
    int main()
    {
    	int M, N;
    	
    	cin>>M>>N;
    	
    	for(int i = 1; i <= M; i++)
    	{
    		cin>>arr[i].val;
    		arr[i].pos = i;
    	}
    	
    	sort(arr + 1, arr + M + 1, cmp);
    	
    	int ans = 0;
    	
    	for(int i = 1; i <= N; i++)
    		ans += arr[i].val;
    		
    	cout<<ans<<endl;
    	
    	sort(arr + 1, arr + N + 1, cmp1);
    	
    	if( N == 1)
    		cout<<M<<endl;
    	else
    		for(int i = 1; i <= N; i++)
    		{
    			if(i == 1)
    				cout<<arr[1].pos<<' ';
    			else if(i == N)
    				cout<<M - arr[N - 1].pos<<endl;
    			else
    				cout<<arr[i].pos - arr[i - 1].pos<<' ';
    		}
    		
    	return 0;
    }
  • 相关阅读:
    使用Lua编写Wireshark插件解析KCP UDP包,解析视频RTP包
    开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt
    2019 WebRtc AudioMixer混音流程
    记录一次定位视频通话 音视频卡顿的原因分析过程。
    C++标准库里自带的数值类型和字符串互相转换函数
    C++ raw string literal
    使用multiprocessing解决PyMuPDF不支持多线程加载导致的界面卡死无响应问题,及一个PyQt5实现的简易PDF阅读器例子
    使用ctypes调用系统C API函数需要注意的问题,函数参数中有指针或结构体的情况下最好不要修改argtypes
    使用python uiautomation从钉钉网页版提取公司所有联系人信息
    使用python UIAutomation从QQ2017(v8.9)群界面获取所有群成员详细资料,
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270576.html
Copyright © 2011-2022 走看看