本文已参与「新人创作礼」活动, 一起开启掘金创作之路。
【ICPC】2022西安站 F. Hotel | 贪心
题目链接
题目
You are doing volunteer work for a programming competition in an ancient hotel. Unfortunately, the hotel provides no phone signal or tap water since it can be dated back to the Qin Dynasty, and you have to assign the contestants to the hotel rooms manually instead of using the internet apps. Fortunately, the hotel has sufficient rooms, and you have taken a computer that lets you do some computation locally.
There are n teams, each with exactly 3 contestants. There are 2 types of rooms in the hotel, the single room and double room, which can receive at most 1 and 2 contestants, respectively. To avoid embarrassing contestants, if two contestants are assigned to a double room, they must come from the same team and have the same gender.
The cost of each room of the same type is the same, but different types may have different costs. Your program needs to calculate the minimum price the host has to pay. The teams are waiting in the registration hall now, and the competition finance officer relies on you to save costs and make a fortune by the residual value. Be quick, or the finance officer will sue you for violating his reputation!
题目大意
共有 n 支队伍,每支队伍正好有 3 名参赛者。酒店有两种类型的房间,单人间和双人间,最多可分别接待 1 名和 2 名参赛者。为了避免让参赛者尴尬,如果两名参赛者被分配到一个双人间,他们必须来自同一个团队,并且性别相同。
同一类型的每个房间的成本是相同的,但不同类型的房间可能有不同的成本,单人间的房费为 ,双人间的房费是 。对于给定的 只队伍里人的性别,求最小的安排住宿需要花的钱。
思路
不论一个队伍里性别如何,他们都可以住三个双人间、三个单人间。
如果一个队伍存在两个人性别相同,那么可以给这只队伍分配两个双人间或一个单人间一个双人间。
分别对每个队伍最小化房费并求和即可。
代码
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
char A,B,C;
int main()
{
int n,c1,c2,ans=0;
scanf("%d%d%d",&n,&c1,&c2);
for (int i=1,x;i<=n;++i)
{
cin>>A>>B>>C;
x=min({3*c1,3*c2});
if (A==B||B==C||A==C) x=min({x,2*c2,c2+c1});
ans+=x;
}
cout<<ans;
return 0;
}