P8072 [COCI2009-2010#7] COKOLADA 题解

· · 题解

思路

原文

First, note that the smallest possible size that contains K squares is the smallest power of 2 larger than or equal to K.

Greedy strategy yield the smallest number of breaks required to reduce 2^X to K. First we break the piece in two, yielding two pieces, both smaller than K,with 2^{x-1} squares each. We now take one of them and need K-2^{x-1} squares more of the other one. We break that one in half and repeat the process. Each time we need more than half of the current square, we keep one part. On other occasion we throw that part away.

翻译

首先,请注意包含 K 个单位的正方形肯定是最小的大于或等于 K2 的整数次幂。

贪心策略是将 2^X 减小到 K 的最少操作次数。首先,我们将一部分分成两块,产生两个新的部分。我们取其中的一个,需要另一个中 K-2^{x-1} 单位的巧克力。我们再把它一分为二,重复这个操作。如果需要的单位数小于一半的个数,那么将其中的一块一分为二,另一块丢弃。

代码

#include <cstdio>

int main() {
  int n, k, d;
  scanf("%d", &n);
  k = n & -n;
  for( d = 0; k < n; k <<= 1 ) ++d;
  printf("%d %d\n", k, d);
  return 0;
}