跳到正文

题解

题解:P12146 【MX-X11-T0】「蓬莱人形 Round 1」糖果曲奇巧克力

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

文章信息

发布
2025年4月12日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 2 分钟
题目
P12146

版本

  1. v1当前

题目传送门


一:分析题意

注意到问题是删光糖果或删光曲奇,所以可以只考虑一种食物。

于是,分类讨论:

  1. 选择删糖果,两个两个删,花费 ⌊a2⌋×x+a mod 2×y\lfloor \frac{a}{2} \rfloor × x + a \bmod 2 × y ;
  2. 选择删糖果,一个一个删,花费 a×ya × y ;
  3. 选择删曲奇,两个两个删,花费 ⌊b2⌋×x+b mod 2×y\lfloor \frac{b}{2} \rfloor × x + b \bmod 2 × y ;
  4. 选择删曲奇,一个一个删,花费 b×yb × y ;

这里举1,2两例说明:

  1. 两个两个删,最多删 ⌊a2⌋\lfloor \frac{a}{2} \rfloor 次,每次花费 xx ,剩下 a mod 2a \bmod 2 个,每次花费 yy ,共花费 ⌊a2⌋×x+a mod 2×y\lfloor \frac{a}{2} \rfloor × x + a \bmod 2 × y 。
  2. 一个一个删,最多删 aa 次,每次花费 yy ,共花费 a×ya × y 。

3,4两例同上,看不懂的这辈子有了把上面的糖果换成曲奇, aa 换成 bb 。

注意:
这里的四种方法有的并不能满足题目要求(例如至少有两个糖果或至少有两个曲奇才能进行此操作),但题目数据有解,所以无伤大雅。

求出了四个值,由于题目中要求最小代价,所以对四个数求最小值即可。


二:AC code

以下使用c++代码完成。

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <algorithm>
#define max min    // 注意,个人习惯
#define luogu long long
using namespace std;

int main() {
	luogu t;
	cin >> t;
	while(t--) {
		luogu a, b, x, y;
		cin >> a >> b >> x >> y;
		luogu ans1, ans2, ans3, ans4;
		ans1 = a / 2 * x + a % 2 * y;
		ans2 = a * y;
		ans3 = b / 2 * x + b % 2 * y;
		ans4 = b * y;
		cout << max(ans1, max(ans2, max(ans3, ans4))) << "\n";  
	}
	return 0;
}
原文