第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)C-Stone Game

题目

题目描述
MianKing has nn piles of stones and each pile has at most 3 stones, now he wants to merge all of these stones into one pile.

In order to achieve his goal, each time MianKing can choose two piles of stones and merge them into a new pile, and the number of stones in the new pile is the sum of these two piles.

Because it takes manpower to move stones, in each operation if the numbers of these two piles of stones are xx and yy respectively, MianKing should pay ( x   m o d   3 ) ( y   m o d   3 ) (x~mod~3)(y~mod~3) (x mod 3)(y mod 3)coins for it.

Now MianKing wants to know the minimum amount of coins he need to pay to merge all of these stones into one pile.

在这里插入图片描述

题意

这题题意比较绕,一上来还读错了。。
给你 a 1 a_1 a1堆由1个石头组成的石堆, a 2 a_2 a2堆由2个石头组成的石堆, a 3 a_3 a3堆由3个石头组成的石堆,每次合并两个石头数为X和Y石头堆的费用是 ( x % 3 ) ∗ ( y % 3 ) (x \% 3) * (y \% 3) (x%3)(y%3) 最小化把所有石头堆合并成一堆的费用。

思路

很显然三个一堆的石头堆不需要管,因为本身mod3就等于0,合并的时候不会产生费用。
把三个1合并成一个3 需要 1 + 2 = 3 的费用 而把三个2合并成一个6需要 2 + 4 = 6的费用,而直接合并1和2只需要2的费用,那么我们考虑先合并1 2 剩下不能合并的如果存在两堆,那么把它们合并以后等待其他mod3 = 0 的堆花费0费用合并完再和这个大堆合并 所以多余的费用就是把剩下两个堆合并的费用。

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
	ll a,b,c;
	cin>>a>>b>>c;
	ll now = min(a,b);
	ll ans = now * 2;
	a -= now;
	b -= now;
	if(a) // 如果a有剩余 
	{
		ll cnt = a/3;
		ans += cnt * 3; // 1 + 2
		a %= 3;
		if(a == 2) ans += 1; // 如果还剩下两个的话 就合并一下,等所有mod3=0的合在一起再合并不消耗更多的钱
						//如果a=1的话 就到最后合并一下,不消耗更多的钱了 
	}
	else // 如果b有剩余 
	{
		ll cnt = b/3;
		ans += cnt * 6; // 2 + 4
		b %= 3;
		if(b == 2) ans += 4; // 和上面同理 
	}
	cout<<ans;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值