跳到正文

题解

题解 P2677 【超级书架 2】

Sioncovy约 2 分钟
原文
文章信息与版本

文章信息

发布
2018年11月7日
收录
2026年9月29日
更新
2026年9月29日
篇幅
约 2 分钟
获赞
8
题目
P2677

版本

  1. v1当前

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

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

C++
# 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;
}

玄学大法好啊

原文
抓取于
  1. zhzzhzzzz

    orz您太神了

  2. hjxhjx

    6666orz

  3. #SUSPENDED#

    还没有评论