我是真的菜,开始进行回复性训练。做了一个水题,敲了好长时间,反思一下就是没有思考成熟就开始干了。结合最近看的书,一定要认清思维的表象。思考成熟后,在开始写。
#include<cstdio>
#include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
const int MAXN = 2e3 + 10;
bool cmp1(pair<int,int> a,pair<int,int> b)
{
if(a.first == b.first)
return a.second < b.second;
else
return a.first > b.first;
}
bool cmp2(pair<int,int> a,pair<int,int> b)
{
return a.second < b.second;
}
int main()
{
int n,k;
pair<int,int> a[MAXN];
pair<int,int> b[MAXN];
while(cin >> n >> k)
{
int ans = 0;
for(int i = 0;i < n;++i)
{
cin >> a[i].first;
a[i].second = i + 1;
}
if(n == 1)
{
for(int i = 0;i < n;++i) ans += a[i].first;
cout << ans << endl;
cout << "1" << endl;
continue;
}
sort(a,a + n,cmp1);
for(int i = 0;i < k;++i) ans += a[i].first;
for(int i = 0;i < k;++i) b[i] = a[i];
sort(b,b + k,cmp2);
sort(a,a + n,cmp2);
cout << ans << endl;
int temp[MAXN];
int t = 0,cnt = 0;
for(int i = 0;i <= n;++i)
{
cnt++;
if(a[i] == b[t] && t != k - 1)
{
temp[t++] = cnt;
cnt = 0;
}
}
temp[t] = n - b[k - 2].second;
for(int i = 0;i < k;++i)
{
if(!i) cout << temp[i];
else cout << " " << temp[i];
}
cout << endl;
}
return 0;
}