题目链接:​​Planning​

题目大意:有n架飞机,所有的飞机至少得在k+1分钟后才能飞行,第i架飞机每分钟延迟的费用是a[i],每分钟只能起飞一架飞机,问最小延迟费用是多少

题目思路:按样例模拟,我们可以看到对于延迟花费最大的,我们应该让他在最接近的时候走,然后贪心做,用过的时间不能再用,所以要扔出去,用一个set维护就好了,思路比较乱,看代码一眼就能看懂了

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 3e5+10;

ll n,k,ans[maxn];
struct node{
ll n1;
ll n2;
ll se;
}num[maxn];

bool cmp(node a,node b){
if(a.n1 == b.n1) return a.n2 > b.n2;
return a.n1 > b.n1;
}

int main(){
while(~scanf("%I64d%I64d",&n,&k)){
for(ll i = 0;i < n;i++){
scanf("%I64d",&num[i].n1);
num[i].n2 = i+1;
num[i].se = i;
}
sort(num,num+n,cmp);
set<ll>s;
set<ll>::iterator it;
ll sum = 0;
for(ll i = 1;i <= n;i++) s.insert(i);
for(ll i = 0;i < n;i++){
ll p = *s.lower_bound(num[i].n2-k);
sum = sum + (p+k-num[i].n2)*num[i].n1;
ans[num[i].se] = p+k;
s.erase(p);
}
printf("%I64d\n",sum);
for(ll i = 0;i < n;i++) printf("%I64d ",ans[i]);
puts("");
}
return 0;
}