HDU 4811 Ball 贪心
题目链接:
题目
Ball
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
问题描述
Jenny likes balls. He has some balls and he wants to arrange them in a row on the table.
Each of those balls can be one of three possible colors: red, yellow, or blue. More precisely, Jenny has R red balls, Y yellow balls and B blue balls. He may put these balls in any order on the table, one after another. Each time Jenny places a new ball on the table, he may insert it somewhere in the middle (or at one end) of the already-placed row of balls.
Additionally, each time Jenny places a ball on the table, he scores some points (possibly zero). The number of points is calculated as follows:
1.For the first ball being placed on the table, he scores 0 point.
2.If he places the ball at one end of the row, the number of points he scores equals to the number of different colors of the already-placed balls (i.e. expect the current one) on the table.
3.If he places the ball between two balls, the number of points he scores equals to the number of different colors of the balls before the currently placed ball, plus the number of different colors of the balls after the current one.
What's the maximal total number of points that Jenny can earn by placing the balls on the table?
输入
There are several test cases, please process till EOF.
Each test case contains only one line with 3 integers R, Y and B, separated by single spaces. All numbers in input are non-negative and won't exceed 109.
输出
For each test case, print the answer in one line.
样例
input
2 2 2
3 3 3
4 4 4
output
15
33
51
题意
给你三种颜色的气球各若干个。现在把所有的气球放成一排,放第一个没有贡献值,之后每放一个气球,贡献值等于放在这个气球左边的不同颜色种类数加上放在这个气球右边的不同颜色种类数。最后要求一种摆放方案使得所有贡献值的总和的最大值。
题解
各种分类讨论的挫代码orz:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int main() {
int tc,kase=0;
LL a[22];
while(scanf("%lld%lld%lld",&a[0],&a[1],&a[2])==3) {
sort(a,a+3);
if(a[0]>=2&&a[1]>=2&&a[2]>=2) {
LL ans=15+6*(a[0]+a[1]+a[2]-6);
printf("%lld\n",ans);
} else if(a[0]>=1&&a[1]>=1&&a[2]>=1){
a[0]--; a[1]--; a[2]--;
LL ans=3;
if(a[1]>=1&&a[2]>=1){
ans+=7+5*(a[1]+a[2]-2);
}else if(a[2]>0){
ans+=3+(a[2]-1)*4;
}
printf("%lld\n",ans);
}else{
LL ans=0;
if(a[1]>=2&&a[2]>=2){
ans=6+(a[1]+a[2]-4)*4;
}else if(a[1]>=1&&a[2]>=1){
ans=1;
a[1]--; a[2]--;
if(a[2]>0){
ans+=2+(a[2]-1)*3;
}
}else{
a[2]--;
if(a[2]>0){
ans=1+2*(a[2]-1);
}else{
ans=0;
}
}
printf("%lld\n",ans);
}
}
return 0;
}
优化之后的:(这很优化)
首先,每种颜色取出两个(不足两个的有几个选几个),易知先取出来的这几个数可以取到最好的情况(一个等差数列),之后我们采取贪心的策略在中间放,同样也是最优的。 假设我们能先取出x个(x<=6),那我们形成的最优序列就是:1 2 3 4 5 5 5 5 ... 5; 答案就是:x(x-1)/2+x(sum-x);
#include<stdio.h>
#include<algorithm>
typedef long long LL;
int main(){
int a[3];
while(scanf("%d%d%d",&a[0],&a[1],&a[2])==3){
LL cnt=0;
for(int i=0;i<3;i++) cnt+=std::min(2,a[i]);
cnt=cnt*(cnt-1)/2+cnt*((LL)a[0]+a[1]+a[2]-cnt);
printf("%lld\n",cnt);
}
return 0;
}
HDU 4811 Ball 贪心的更多相关文章
- hdu 5821 Ball 贪心
Ball 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个n一个m,在给定两个长度为n的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组. 题 ...
- HDU 4811 Ball -2013 ICPC南京区域现场赛
题目链接 题意:三种颜色的球,现给定三种球的数目,每次取其中一个放到桌子上,排成一条线,每次放的位置任意,问得到的最大得分. 把一个球放在末尾得到的分数是它以前球的颜色种数 把一个球放在中间得到的分数 ...
- [思考] hdu 4811 Ball
意甲冠军: 有三种颜色的小珠,每种颜色的量R,Y,B 转球进入桌面成序,有多少种不同的颜色分别砍下的球在球门前+有多少身后球不同的颜色 问:最大的总比分值 思考: 球和后面的球先放好.剩下的就放中间了 ...
- HDU - 4811 - Ball (思维)
题意: 给出一定数量的三种颜色的球,计算如何摆放得到值最大(有一定顺序) 有三种摆放方法 1.如果放的是第一个(桌子上原来没有),数值不变 2.如果在末尾追加一个,那么增加前面不同颜色的个数的值 3. ...
- Hdu 4864(Task 贪心)(Java实现)
Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- HDU 4362 Dragon Ball 贪心DP
Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon ...
随机推荐
- Java之阶乘数的计算
说起“阶乘数”,我们应该都不会感到陌生.当老师布置了这样的作业,我们大多数人是一贯用笔算,还有的同学会用计算机去计算.数学是讲究原理和方法的,我们知其然,也要知其所以然.下面我们就用编程来计算阶乘数. ...
- OS中常用的调度算法总结 (转)
http://blog.chinaunix.net/uid-25132162-id-361291.html 一.常见的批处理作业调度算法 1.先来先服务调度算法(FCFS):就是按照各个作业进入系统的 ...
- OSPF路由汇总和默认路由设置
目标 掌握OSPF路由汇总的配置 掌握OSPF默认路由的配置 一.——区域间汇总 配置IP,R2四个环回口 R1(config)#inter s1/0 R1(config-if)#ip add 200 ...
- html5的自定义data-*属性和jquery的data()方法的使用示例
人们总喜欢往HTML标签上添加自定义属性来存储和操作数据. 但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它 ...
- asp.net 小技巧
文字用一个label标签包起来,设置一个属性:for,其for的值要和复选框的id相同. <p> 1.通过点击文字,就选中复选框</p> <p>文字用一个label ...
- AMQ学习笔记 - 05. 客户端模板化
概述 客户端编程模型中,大部分的步骤都是相同的.将相同的部分做成模板,将不同的部分预留接口,实现者就只需要针对不同的部分提供实现. 设计 类图 发送方客户端 说明: 基于模板的思想,SendTempl ...
- [Guava源码分析] Preconditions 前置条件
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3874170.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- <Linux系统hostname命令详解>
hostname命令的用法的小知识我们都知道hostname命令是查看主机名和修改主机名的. [root@apache ~]# hostname //查看本机的主机名apache.example.c ...
- Spring MVC的启动过程
一.概述 下面一个基本的运用springMVC的的web.xml的配置,这里要注意两个地方,一个是ContextLoadListener,一个是DispatcherServlet.web容器正是通过这 ...
- SQL Server Profiler监控执行语句
SQL Server Profiler监控执行语句,这个功能主要用在实时的监控对数据库执行了什么操作,从而及时有效的跟踪系统的运行. 常规配置选项,名称.模板.保存到文件(可以复用). 事件选择,可以 ...