Codeforces 854C Planning【贪心+并查集】

来源:互联网 发布:机械模拟仿真软件 编辑:程序博客网 时间:2024/05/29 05:56

C. Planning
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n flights that must depart today, the i-th of them is planned to depart at the i-th minute of the day.

Metropolis airport is the main transport hub of Metropolia, so it is difficult to keep the schedule intact. This is exactly the case today: because of technical issues, no flights were able to depart during the first k minutes of the day, so now the new departure schedule must be created.

All n scheduled flights must now depart at different minutes between (k + 1)-th and (k + n)-th, inclusive. However, it's not mandatory for the flights to depart in the same order they were initially scheduled to do so — their order in the new schedule can be different. There is only one restriction: no flight is allowed to depart earlier than it was supposed to depart in the initial schedule.

Helen knows that each minute of delay of the i-th flight costs airport ci burles. Help her find the order for flights to depart in the new schedule that minimizes the total cost for the airport.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 300 000), here n is the number of flights, and k is the number of minutes in the beginning of the day that the flights did not depart.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 107), here ci is the cost of delaying the i-th flight for one minute.

Output

The first line must contain the minimum possible total cost of delaying the flights.

The second line must contain n different integers t1, t2, ..., tn (k + 1 ≤ ti ≤ k + n), here ti is the minute when the i-th flight must depart. If there are several optimal schedules, print any of them.

Example
input
5 24 2 1 10 2
output
203 6 7 4 5 
Note

Let us consider sample test. If Helen just moves all flights 2 minutes later preserving the order, the total cost of delaying the flights would be (3 - 1)·4 + (4 - 2)·2 + (5 - 3)·1 + (6 - 4)·10 + (7 - 5)·2 = 38 burles.

However, the better schedule is shown in the sample answer, its cost is (3 - 1)·4 + (6 - 2)·2 + (7 - 3)·1 + (4 - 4)·10 + (5 - 5)·2 = 20


题目大意:

给出一个长度为N的序列,表示每个位子的val【i】;

然后给出N个数,从K+1~K+N,让我们将这n个数和1~N进行匹配,匹配的价值为(k+x-i)*val【i】;

让你找到一个最小总价值和,要求k+x-i>=0


思路:


这个题显然是一个贪心的题目,那么对应我们需要将序列按照val【i】从大到小排序,然后我们从大到小遍历,每个位子我们都求当前情况下的最优解(也就是找比当前位子i大的第一个数);

我们显然直接做时间复杂度是O(n^2)的,那么这里我们可以二分优化查找速度,也可以用并查集优化一下。

代码给出的是并查集优化的做法,具体参考一下代码即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll __int64struct node{    ll val,pos;}a[605000];ll f[605000];ll b[605000];ll cmp(node a,node b){    return a.val>b.val;}ll find(ll a){    ll r=a;    while(f[r]!=r)    r=f[r];    ll i=a;    ll j;    while(i!=r)    {        j=f[i];        f[i]=r;        i=j;    }    return r;}void merge(ll a,ll b){    ll A,B;    A=find(a);    B=find(b);    if(A!=B)    {        if(A<B)        {            f[A]=B;        }        else f[B]=A;    }}ll ans[650000];int main(){    ll n,k;    while(~scanf("%I64d%I64d",&n,&k))    {        ll output=0;        for(ll i=k+1;i<=n+k;i++)f[i]=i;        for(ll i=1;i<=k;i++)merge(i,i+1);        for(ll i=1;i<=n;i++)scanf("%I64d",&a[i].val),a[i].pos=i;        sort(a+1,a+1+n,cmp);        for(ll i=1;i<=n;i++)        {            output+=a[i].val*(find(a[i].pos)-a[i].pos);            ans[a[i].pos]=find(a[i].pos);            merge(find(a[i].pos),find(a[i].pos)+1);        }        printf("%I64d\n",output);        for(ll i=1;i<=n;i++)printf("%I64d ",ans[i]);        printf("\n");    }}