1. 程式人生 > >【Atcoder - AGC027B】Garbage Collector

【Atcoder - AGC027B】Garbage Collector

@Garbage [email protected]


@題目描述 - [email protected]

Time limit : 2sec / Memory limit : 1024MB

Score : 700 points

Problem Statement
Snuke has decided to use a robot to clean his room.

There are N pieces of trash on a number line. The i-th piece from the left is at position xi. We would like to put all of them in a trash bin at position 0.

For the positions of the pieces of trash, 0 < x1 < x2 < … < xN ≤ 10^9 holds.

The robot is initially at position 0. It can freely move left and right along the number line, pick up a piece of trash when it comes to the position of that piece, carry any number of pieces of trash and put them in the trash bin when it comes to position 0. It is not allowed to put pieces of trash anywhere except in the trash bin.

The robot consumes X points of energy when the robot picks up a piece of trash, or put pieces of trash in the trash bin. (Putting any number of pieces of trash in the trash bin consumes X points of energy.) Also, the robot consumes (k+1)^2 points of energy to travel by a distance of 1 when the robot is carrying k pieces of trash.

Find the minimum amount of energy required to put all the N pieces of trash in the trash bin.

Constraints
1≤N≤2×10^5
0 < x1 < … < xN≤10^9
1≤X≤10^9
All values in input are integers.

Partial Scores
400 points will be awarded for passing the test set satisfying N≤2000.

Input
Input is given from Standard Input in the following format:
N X
x1 x2 … xN

Output
Print the answer.

Sample Input 1
2 100
1 10
Sample Output 1
355

Travel to position 10 by consuming 10 points of energy.
Pick up the piece of trash by consuming 100 points of energy.
Travel to position 1 by consuming 36 points of energy.
Pick up the piece of trash by consuming 100 points of energy.
Travel to position 0 by consuming 9 points of energy.
Put the two pieces of trash in the trash bin by consuming 100 points of energy.
This strategy consumes a total of 10+100+36+100+9+100=355 points of energy.

Sample Input 2
5 1
1 999999997 999999998 999999999 1000000000
Sample Output 2
19999999983

Sample Input 3
10 8851025
38 87 668 3175 22601 65499 90236 790604 4290609 4894746
Sample Output 3
150710136

Sample Input 4
16 10
1 7 12 27 52 75 731 13856 395504 534840 1276551 2356789 9384806 19108104 82684732 535447408
Sample Output 4
3256017715

@中文題意@

一個機器人從原點出發,要清掃在座標軸上的垃圾。原點有一個垃圾桶。機器人可以在座標軸上左右移動,當移動到某個垃圾的位置上時,可以選擇花費 X 點能量將它撿起來(也可以視而不撿)。機器人如果到達垃圾桶,則可以將它攜帶的垃圾花費 X 點能量倒出。機器人如果攜帶著 K 件垃圾移動一個單位距離,則需要消耗 (K+1)^2 點能量。

問將所有垃圾全部弄到垃圾桶裡面去所需消耗的最小能量。

@分析@

考慮到代價如果是由兩部分(撿垃圾扔垃圾,移動)組成的不好考慮,我們可以分開處理。顯然每個垃圾都需要撿起來,則至少有N*X點花費。假如我們去倒了K次垃圾,則有K*X點花費。

我們考慮某一次倒垃圾的過程:先從原點出發,再按照一定順序遍歷完某一個集合 S 的點,最後回到原點。嘛……這裡有一個貪心性質但我不會證明:我們先從原點直接到達距離最遠的點,再往回走,在歸程中收集垃圾並在原點的垃圾桶倒垃圾。

這樣代價怎麼表示出來呢?令集合 S 的點依次為X[1], X[2], X[3] ….X[s]。如圖所示:
解釋圖

於是?總花費等於 = 每個點的 X 座標 * 一個係數。然後又是一個貪心性質:因為我們倒了K次垃圾,所以最多有2*K個點的係數為5,最多有K個點的係數為7…..我們將較遠的點賦一個較小的係數,可以發現這樣一定是最優的。

K的取值從1到N,每一種類係數可以用字首和O(1)弄出,對於每個K有N/K種系數要考慮。則一個經典的時間複雜度O(N/1+N/2+N/3+…+N/N) = O(NlogN)
【注意會溢位long long!】
【注意會溢位long long!】
【注意會溢位long long!】

@程式碼@

【跪在第一步QAQ……】
【不過還好考場上我跳過了這道題qwq】
如果有什麼不懂的地方可以留言在下面問我哦~我會盡力解答的OuO

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 200000;
ull sum[MAXN + 5];
int main() {
    int N; ull X;
    cin >> N >> X;
    for(int i=1;i<=N;i++) {
        cin >> sum[i];
        sum[i] += sum[i-1];
    }
    ull ans = X*N + sum[N]*5;
    for(int i=1;i<N;i++) {
        ull res = X*i;
        if( 2*i >= N )
            res = res + sum[N]*5;
        else {
            ull coef = 7;
            int lst = N - 2*i;
            res = res + (sum[N]-sum[N-2*i])*5;
            while( lst >= i ) {
                res = res + (sum[lst]-sum[lst-i])*coef;
                lst -= i; coef += 2;
            }
            res = res + sum[lst]*coef;
        }
        if( res < ans ) ans = res;
    }
    cout << ans + X*N;
} 

@[email protected]

就是這樣,新的一天裡,也請多多關照哦(ノω<。)ノ))☆.。~