题目链接
思路:贪心+排序
每次往k+i的位置加当前未被加过的最大值, 应该好好考虑在写的,写了好久说。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <vector>
using namespace std;
const int MAXN = 300005;
typedef long long LL;
struct node
{
LL num;
int id;
}a[MAXN];
LL b[MAXN];
LL ans[10000000+10];
bool cmp(node a, node b)
{
return a.num>b.num;
}
int main(){
int n, k;
while(~scanf("%d %d", &n, &k))
{
memset(b, 0, sizeof(b));
memset(ans, 0, sizeof(ans));
for(int i=1;i<=n;++i)
{
scanf("%lld", &a[i].num);
b[i]=a[i].num;
a[i].id=i;
}
sort(a+1, a+1+n, cmp);
LL sum=0;
int cnt=k+1;
int i=1;
while(1)
{
if(cnt==k+n+2)
break;
while(ans[cnt]!=0)
cnt++;
if(a[i].id<=cnt)
{
ans[a[i].id]=cnt;
cnt++;
i++;
}
else
{
ans[a[i].id]=a[i].id;
i++;
}
}
for(int i=1;i<=n;++i)
sum+=b[i]*(ans[i]-i);
cout<<sum<<endl;
for(int i=1;i<=n;++i)
printf("%lld%c", ans[i], " \n"[i==n]);
}
return 0;
}