CF Round433 C. Planning

题目链接http://codeforces.com/contest/854/problem/C

题目大意:有N个航班,第i个在第i分钟起飞i >= 1。现在由于不可知原因,要求从第1分钟开始到第k分钟不能有飞机起飞,所以每个飞机都得向后延误一段时间,但是每个航班每延误1分钟有c[i]的代价并且对于原计划第x分钟起飞的飞机,延误之后不能早于第x分钟起飞,问如何安排代价最小。

解题思路:贪心的找前k+i分钟内代价最大的可以保证最优解。所以我们使用线段树维护序列最大值和对应id,每次查询前k+i的最大值,累加之后将该值更新为0即可。

代码:

 1 const int inf = 0x3f3f3f3f;
 2 const int maxn = 3e5 + 5;
 3 PII tree[maxn * 4];
 4 int n, k;
 5 int c[maxn], ansv[maxn];
 6 
 7 void build(int l, int r, int k){
 8     if(l == r) {
 9         tree[k] = PII(c[l], l);
10         return;
11     }
12     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
13     build(l, mid, lc);
14     build(mid + 1, r, rc);
15     tree[k] = max(tree[lc], tree[rc]);
16 }
17 void update(int x, int v, int l, int r, int k){
18     if(l == r && x == l){
19         tree[k].first = v; 
20         return;
21     }
22     if(x < l || x > r) return;
23     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
24     update(x, v, l, mid, lc);
25     update(x, v, mid + 1, r, rc);
26     tree[k] = max(tree[lc], tree[rc]);
27 }
28 PII query(int ql, int qr, int l, int r, int k){
29     if(ql <= l && qr >= r){
30         return tree[k];
31     }
32     if(ql > r || qr < l) return PII(0, 0);
33     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
34     PII q1 = query(ql, qr, l, mid, lc);
35     PII q2 = query(ql, qr, mid + 1, r, rc);
36     return max(q1, q2);
37 }
38 void solve(){
39     build(1, n, 1);
40     ll ans = 0;
41     for(int i = 1; i <= n; i++){
42         int q = i + k;
43         PII tmp = query(1, q, 1, n, 1);
44         ll tp1 = tmp.first, tp2 = tmp.second;
45         ans += tp1 * (q - tp2);
46         ansv[tmp.second] = q;
47         update(tmp.second, 0, 1, n, 1);
48     }
49     printf("%lld
", ans);
50     for(int i = 1; i <= n; i++) printf("%d ", ansv[i]);
51     puts("");
52 }
53 int main(){
54     scanf("%d %d", &n, &k);
55     for(int i = 1; i <= n; i++){
56         scanf("%d", &c[i]);
57     }
58     solve();
59 }

题目:

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 2
4 2 1 10 2
output
20
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 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 = 20burles.

原文地址:https://www.cnblogs.com/bolderic/p/7489168.html