链接:https://www.nowcoder.com/acm/contest/143/J

来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 262144K,其他语言524288K

64bit IO Format: %lld

题目描述

There are n students going to travel. And hotel has two types room:double room and triple room. The price of a double room is p2 and the price of a triple room is p3

Now you need to calulate the minimum total cost of these students.

输入描述:

The first line has three integers n, p2, p3

输出描述:

Output the minimum total cost.

示例1

输入

4 2 3

输出

4

示例2

输入

5 1 3

输出

3

备注:

1<=n<=10^9

1<=p2,p3<=10^9

题意

n个人住宾馆,二人房价钱p2,三人房价钱p3 。求这n个人全部住进宾馆的最少花费

思路

暴力进行判断

先对1个人进行特判:最少的花费肯定是min(p2,p3)。

然后因为二人房和三人房有一定的性价比(即平均每个人的住房花费)。所以入住的时候肯定要尽可能多的选择性价比高的房间(平均每人花费最少),所以可以选择:全部是二人间,全部是三人间,如果有剩余,让剩下的人住二人间或三人间,对于所有的情况取最小值就是最小花费。

大概就是这样,代码里写的有点麻烦,好像有些情况用不到,这样写情况容易考虑不全,WA了15次之后才把所有的情况写全。不知道大佬们都是怎么写的。如果有更好的思路,欢迎交流٩(๑>◡<๑)۶

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
ll n,p2,p3;
ll ans=0;
ll flag1=0,flag2=0;
while(cin>>n>>p2>>p3)
{
flag1=n%2;
flag2=n%3;
if(n==1)
{
cout<<min(p2,p3)<<endl;
continue;
}
if(flag1==0&&flag2==0)
ans=min(n/2*p2,n/3*p3);
else if(flag1==0&&flag2)
{
ll x=n/3;
if(flag2==2)
ans=min(n/2*p2,min(x*p3+p2,min((x-1)*p3+3*p2,(x+1)*p3)));
if(flag2==1)
ans=min(n/2*p2,min(x*p3+p2,min((x+1)*p3,min(p2*(n/2-1)+p3,p3*(x-1)+p2*2))));
}
else if(flag1&&flag2==0)
{
ll y=n/2;
ans=min((y+1)*p2,min(n/3*p3,(y-1)*p2+p3));
}
else if(flag1&&flag2)
{
ll x=n/2;
ll y=n/3;
if(flag2==1)
ans=min((x+1)*p2,min((y+1)*p3,min((y-1)*p3+2*p2,min(p2*(x-1)+p3,y*p3+p2))));
else if(flag2==2)
ans=min((x+1)*p2,min((y+1)*p3,min(y*p3+p2,min((x-1)*p2+p3,(y-1)*p3+3*p2))));
}
cout<<ans<<endl;
}
return 0;
}

牛客多校第五场 J:Plan的更多相关文章

  1. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  2. 牛客多校第六场 J Heritage of skywalkert 随即互质概率 nth_element(求最大多少项模板)

    链接:https://www.nowcoder.com/acm/contest/144/J来源:牛客网 skywalkert, the new legend of Beihang University ...

  3. 牛客多校第五场 E room 二分图匹配 KM算法模板

    链接:https://www.nowcoder.com/acm/contest/143/E来源:牛客网 Nowcoder University has 4n students and n dormit ...

  4. 牛客多校第五场 F take

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...

  5. 牛客多校第五场-D-inv

    链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...

  6. 牛客多校第五场 F take 期望转化成单独事件概率(模板) 树状数组

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 Kanade has n boxes , the i-th box has p[i] proba ...

  7. 字符串dp——牛客多校第五场G

    比赛的时候脑瘫了没想出来..打多校以来最自闭的一场 显然从s中选择大于m个数组成的数必然比t大,所以只要dp求出从s中选择m个数大于t的方案数 官方题解是反着往前推,想了下反着推的确简单,因为高位的数 ...

  8. 【魔改】树状数组 牛客多校第五场I vcd 几何+阅读理解

    https://www.nowcoder.com/acm/contest/143/I vc-dimension 题解:分三种情况,组合数学算一下,其中一种要用树状数组维护 技巧(来自UESTC):1. ...

  9. 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)

    题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...

随机推荐

  1. Codeforces C - Om Nom and Candies

    C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否 ...

  2. 在每个节点填充向右的指针 Populating Next Right Pointers in Each Node

    2018-08-09 16:01:40 一.Populating Next Right Pointers in Each Node 问题描述: 问题求解: 由于是满二叉树,所以很好填充. public ...

  3. Java JDK5新特性-泛型

    2017-10-30 22:47:11 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质 ...

  4. SQL脚本去重分组统计

    需求:首先有一张表记录学生姓名.科目和成绩,然后模拟插入几条数据,脚本如下: create table score ( Name ),--姓名 subject ),--科目 grade int--成绩 ...

  5. JS-Object(3) 继承(prototype方式, 类方式); javascript6的知识(部分)

    原型方式的继承 创建child object classes(constructors) , 子类如何从父类中继承特性. 原型链继承prototypal inheritance (ruby中的继承也是 ...

  6. 3-18/19 (自我练习)30多个《Ruby元编程》的spell(pattern)小例子。

    Spell,也称pattern,idiom # Around Alias:从一个重新定义的方法中调用原始的,被重命名的版本. # old_reverse是未改变的原始方法,reverse/new_re ...

  7. kubectl管理多个k8s集群

    #把每个k8s集群的json配置文件放到/root/.kube/目录下,改为不同名字,通过--kubeconfig实现不同集群操作 kubectl --kubeconfig=/root/.kube/m ...

  8. juqery的一些简单用法

    层级选择器(重点).基本过滤选择器 :eq(index) 选择匹配到的元素中索引号为index的一个元素,index从0开始 :odd 选择匹配到的元素中索引号为奇数的所有元素,index从0开始 : ...

  9. OAF中下载附件之后页面失效,报过时的数据异常,浏览器后退异常

    我在使用了下载功能之后,再往页面添加行或进行保存,页面老是报浏览器后退的异常. 猜测是因为我的下载按钮使用的submitButton,它隐式的包含了一个submit动作,且我在代码中有一个Commit ...

  10. arcTo

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...