Codeforces 854C. Planning

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 kminutes 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.

读完题易知肯定让推迟代价最大的航班越早走越好,所以实际就是维护一个优先队列,维护当前能走的最大代价的航班

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#define SIZE 300005

struct node{
  LL pos;
  LL c;

  friend bool operator >(const node& lhs,const node& rhs);
//  friend bool operator <(const node& lhs,const node& rhs);
};

bool operator >(const node &lhs,const node& rhs){
  if (lhs.c == rhs.c){
    return (lhs.pos < rhs.pos);
  }
  return (lhs.c < rhs.c);
}
// bool operator <(const node& lhs,const node& rhs){
//   return (lhs.c < rhs.c);
// }

priority_queue<node,vector<node>,greater<node> > pq;
LL n,k,res = 0;
LL ans[SIZE] = {0};

int main()
{
  // freopen("test.in","r",stdin);
  scanf("%I64d %I64d",&n,&k);

  for (int i=1;i<=k+n;i++){
    node temp;
    if (i <= n) {
      LL c;
      scanf("%I64d",&c);
      temp.pos = i; temp.c = c;
      pq.push(temp);
    }
    if (i >= k+1){
      node now = pq.top();
      pq.pop();
      res += now.c * (i - now.pos);
      ans[now.pos] = i;
    }
  }

  printf("%I64d\n",res);
  for (int i=1;i<=n;i++){
    printf("%I64d ",ans[i]);
  }
  return 0;
}

时间: 2024-11-05 13:43:51

Codeforces 854C. Planning的相关文章

Codeforces 854C Planning(贪心+堆)

贪心:让代价大的尽量移到靠前的位置. 做法:先让前k个数加进堆里,枚举k+1~n+k,每次把新元素加进堆后找到最大代价放在当前位置即可. #include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=500010; struct poi{int c,pos;}; priority_queue<poi>q; bool operator<(poi a,poi b){return a

Codeforces 854C Planning 【贪心】

<题目链接> 题目大意: 表示有n架飞机本需要在[1,n]时间内起飞,一分钟只能飞一架.但是现在[1,k]时间内并不能起飞,只能在[k+1,k+n]内起飞.ci序号为i的飞机起飞延误一分钟的costi.每个飞机起飞时间不能比原定时间早,请安排一个起飞顺序,求最小的cost和. 解题分析: 贪心策略证明:转载于>>>设序号为i的飞机起飞时间为di,则cost=∑(di-i)*cj=∑di*cj-∑j*cj.显然后一项为常数,而{di-k}为[1,n]的一个排列, 所以只要使ci

CodeForces - 853A Planning (优先队列,贪心)

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

Codeforces 853A Planning

题意 给出飞机单位晚点时间代价和原定起飞时间,现在前k分钟不能起飞,求付出的最小代价和起飞顺序 思路 构造两个优先队列q1,q2,q1按时间顺序,q2按代价顺序,初始将所有飞机入q1,将时间在k前的飞机从q1加入q2,然后按时间顺序取出q1加入q2,同时从q2取出代价最大的加入答案 代码 #include<bits/stdc++.h> using namespace std; inline int read(){ int x = 0,f= 1; char ch = getchar(); whi

暑假集训 - 8.9 总结

学习内容:set 完成题数:3题 看书情况:6页 做题总结: AcWing 146:https://www.cnblogs.com/buhuiflydepig/p/11326141.html codeforces 854c:https://www.cnblogs.com/buhuiflydepig/p/11329858.html 心得: 不会用一些STL容器,TLE到自闭,vector.erase是O(n), set.erase是O(logn). 原文地址:https://www.cnblogs

Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C. Planning

题意:给出每个航班花费和顺序,求新顺序使得花费最少 思路:肯定是花费最大的先决定,二分一下 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1e5+100; 4 typedef long long ll; 5 6 struct node{ 7 ll x; 8 int id; 9 ll yy; 10 }a[300004]; 11 bool cmp(node p,node q){ 12 return p.x>q.x

Codeforces - 1321B - Journey Planning(思维)

题目链接 题目大意:让你找出一列数,他们中相邻的两个数的下标之差等于数值之差.求这列数的最大值 ??思路很简单,因为所得数列满足相邻的两个数的下标之差等于数值之差.所以只要让每个输入的数减去对应的下标所得到的下标指向的数加上这个数即可,如果两个数的下标之差等于数值之差,那么 它们各自的数减去下标所得到的下标指向的必定是同一个位置.换句话说,这个位置的数等于所有两个数的下标之差等于数值之差的数的和.(这样的位置可以有多个) //https://www.cnblogs.com/shuitiangon

codeforces 505 D Mr. Kitayuta&#39;s Technology

题意:给出n个点,m条有向边,问构造一个新图,最少几条边可以让任意两点间的连通性跟原图一样. 做法:首先做出强连通分量,很显然对于有向图而言,若分图的点不唯一必定成环,当然啦,还需要做的是把这些分图再连起来变成弱连通分量,若某个弱连通分量的点数为v,若有环则贡献v条边,否则贡献v-1条边. #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib

Codeforces Round #433 (Div. 2)

题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 int gcd(int a,int b){re