Codeforces Round #498 (Div. 3) B. Polycarp's Practice
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 nn problems 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.
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).
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.
8 3
5 4 2 6 5 1 9 2
20
3 2 3
5 1
1 1 1 1 1
1
5
4 2
1 2000 2000 2
4000
2 2
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.
题意:给你n个问题,你要在k天内把他解决,每天至少解决一个问题(要按照顺序来解决问题,不能重复解决问题),一天中最困难的问题就是这一天的profit,问总的profit最大是多少。
题解:很容易知道maxsum就是前k个最困难的问题和。每天解决的问题就是每个最困难的问题和上一个最困难的问题之间的问题个数。
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 struct node{ 5 int a; 6 int b; 7 }dian[2010]; 8 bool cmp(node one,node two){ 9 return one.b<two.b;//恢复相对顺序 10 } 11 bool cmp1(node one,node two){ 12 return one.a>two.a;//为了求k项最大的和 13 } 14 int main() 15 { 16 int n,k; 17 scanf("%d%d",&n,&k); 18 for (int i=1;i<=n;i++) 19 { 20 scanf("%d",&dian[i].a); 21 dian[i].b=i; 22 } 23 sort(dian+1,dian+1+n,cmp1); 24 long long sum=0; 25 for (int i=1;i<=k;i++) 26 sum+=dian[i].a; 27 sort(dian+1,dian+1+k,cmp);//最大的k项恢复相对位置。******重点。 28 printf("%d\n",sum); 29 if(k==1) 30 printf("%d\n",n); 31 else 32 { 33 for (int i=1;i<=k;i++)//每天解决的问题就是每个最困难的问题和上一个最困难的问题之间的问题个数。 34 { 35 if(i==1)//第一个困难问题特判一下 36 printf("%d",dian[i].b); 37 else if(i==k)//最后一个困难问题特判一下 38 printf(" %d\n",n-dian[k-1].b); 39 else 40 printf(" %d",dian[i].b-dian[i-1].b); 41 } 42 } 43 }