正睿2020普转提【第六套】塔

T1

题目

睿爸喜欢搭塔塔。

睿爸有(n_1)个高度(h_1)的红色砖块,和(n_2)个高度为(h_2)的蓝色砖块,这些的砖块的底面和顶面的长宽均相同,且你不能将这些砖块立体旋转或者转动。

睿爸可以按照如下方式搭塔:

1.每个砖块要么可以放在地面上,要么必须垒在一个颜色不同的砖块上面(一个砖块上面仅可以放一个砖块)。

2.至少需要一个砖块,不必用完所有的砖块。

睿爸想知道这样最多可以搭出多少个不同高度的塔。

Solution

显而易见的特判题。

  • 使(n_1 > n_2) ,讨论(h_1 、 h_2)是否相等。

  • (h_1 == h_2)时,若(n_1 == n_2),我们发现两块本质等价, 答案为(n_1 + n_2)

  • (h_1 == h_2)时,若(n_1 != n_2),则可以发现答案为(2 imes n_2(设n_1 > n_2) + 1).其中前式不难理解,+1是因为最后还可以叠一块多出的(n_1),形成一种方案。

  • (h_1 != h_2) 时,若(n_1 == n_2),容易发现,奇数个块时把红蓝反转一定能获得另一种方案。

    比如确定蓝色为最低块,一层层往上堆红蓝,到刚好用完,方案为(x = 2 imes n_2(注释同上)).发现以红色为底的话方案数加上(x - x / 2)即x中奇数个数。

  • (h_1 != h_2)时,若(n_1 != n_2), 和第二种情况类似,我们需要再加上1中方案。

(mathrm{Code:})

#include <bits/stdc++.h>
#define int long long
int n1, h1, n2, h2; 

template <class T>
inline void read(T &s) {
	int w = 1;
	char c = getchar();
	while ((c < '0' || c > '9') && c != '-')
    	c = getchar();
    if (c == '-') w = -1, c = getchar();
    while (c <= '9' && c >= '0')
     	s = (s << 1) + (s << 3) + c - '0', c = getchar();
    s *= w;
}
template <class T>
inline void write(T x) {
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + 48);
	return void();	
}

main() {
	freopen("tower.in", "r", stdin);
	freopen("tower.out", "w", stdout);
	read(n1);
	read(h1);
	read(n2);
	read(h2);
	if (n1 < n2) {
		std ::swap(n1, n2);
		std ::swap(h1, h2);
	}
	if (h1 == h2) {
		if (n1 == n2) {
			write(n1 + n2);
			return 0;
		}
		if (n1 != n2) {
			write(2 * std ::min(n1, n2) + 1);
			return 0;
		}
	}
	if (h1 != h2) {
		if (n1 == n2) {
			int x = 2 * std ::min(n1, n2);
			x += (x - x / 2);
			write(x);
			return 0;
		}
		if (n1 != n2) {
			int x = 2 * std ::min(n1, n2);
			x += (x - x / 2);
			write(x + 1);
			return 0;
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/yywxdgy/p/13062403.html