题解 P2677 【超级书架 2】

· · 题解

发现似乎没有玄学做法啊,这怎么能行呢,那我就来骚一波吧讲一下吧。

首先,我们来了解一下 random_shuffle 函数。头文件是<algorithm>,功能就是把一个数组中的数列随机重排,参数自然就是数组的头和尾了。玄学必备啊

# include <iostream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cmath>
# include <vector>
using namespace std;

int read(){
    int x = 0, k = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){
        if(ch == '-')   k = -1;
        ch = getchar();
    }   
    while(ch >= '0' && ch <= '9'){
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * k;
} //快读,无视就好了。

int n, H;//奶牛数量,书架高度
int h[25];//奶牛高度
int minx = 0x7fffffff;//能够达到中的最低高度 - 书架高度

int main(){
    n = read(), H = read();
    for(int i = 1; i <= n; i++)
        h[i] = read();
    for(int cnt = 1; cnt <= 300; cnt++){  //假定重排三百次,对自己rp有自信的可以少点。
        int sum = 0;
        random_shuffle(h + 1, h + n + 1); 
        for(int i = 1; sum < H; i++){
            sum += h[i];
        }
        sum = sum - H;
        minx = min(sum, minx);
    }
    printf("%d\n", minx);
    return 0;
}

玄学大法好啊