Codeforces Round #433 (Div. 2) Planning (greedy + set)

Source: Internet
Author: User
Tags printf time limit

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

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

Metropolis Airport is the main transport hub of Metropolia, so it's difficult to keep the schedule intact. This was 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 is created.

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

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

The first line contains-integers n and K (1≤k≤n≤300), here n are the number of flights, and K is the number O F minutes in the beginning of the "the" and the flights did not depart.

The second line contains n integers c1, c2, ..., CN (1≤ci≤107), here ci are the cost of delaying the i-th flight for on E 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 was the minute when the I-t H Flight must depart. If there is several optimal schedules, print any of them. Example input

5 2
4 2 1 10 2
Output
3 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 is (3- 1) • 4 + (4-2) • 2 + (5-3) • 1 + (6-4) • 10 + (7-5) ·2 = Burles.

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


An obvious greedy topic, priority to postpone the expensive flights, so that it is as low as possible, so that the total cost less. But normal traversal obviously will be tle, so we can use and check the set to skip the time that has been occupied, record the next point of each point is who can.

The specific details code says:

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack > #include <cstring> #include <string> #include <vector> #include <map> #include <cmath
> Using namespace std;
#define MEM (A, B) memset (A,b,sizeof (a)) typedef long Long LL;
const int MAXN = 3E5+5;
const int FF = 0X3F3F3F3F;
	struct Node {ll C;
int POS;

}NODE[MAXN]; 
int n,k;
int ANS[MAXN];

int ne[maxn*2];//This array to open the large bool CMP (struct node a,struct node B) {return a.c> B.C;}
	void Init () {for (int i = 0;i<= maxn*2;i++) ne[i] = i;
return;
	} int find (int x) {int r = x;
	while (x!= ne[x]) x = ne[x];
		while (r!= ne[r])//Must be path compression, otherwise it is easy to timeout {int t = Ne[r];
		NE[R] = x;
	R = t;
} return x;
	} int main () {cin>>n>>k;
	Init ();
		for (int i = 1;i<= n;i++) {scanf ("%i64d", &node[i].c);
	Node[i].pos = i;
	} ll sum = 0;
	Sort (node+1,node+n+1,cmp);
	for (int i = 1;i<= n;i++) {int fa = find (max (node[i].pos,k+1));	Ans[node[i].pos] = FA;
	sum+= (Fa-node[i].pos) *node[i].c;//operation may be super-int, note here Ne[fa] = find (fa+1);
	} cout<<sum<<endl;
		for (int i = 1;i<= n;i++) if (i = = N) printf ("%d\n", Ans[i]);
	else printf ("%d", ans[i]);
return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.