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 ...
随机推荐
- SQL Server高级内容之子查询和表链接
1.子查询概念 (1)就是在查询的where子句中的判断依据是另一个查询的结果,如此就构成了一个外部的查询和一个内部的查询,这个内部的查询就是自查询. (2)自查询的分类 1)独立子查询 ->独 ...
- Ajax请求ashx 返回 json 格式数据常见问题
问题:ashx 返回的字符串json格式,在前台ajax自动解析失败. 问题分析:经过排查,发现是拼接json时出现” ’ “单引号,jquery无法解析,用” “ “双引号才可以.例如: stri ...
- C语言知识总结(4)
变量的作用域 C语言根据变量作用域的不同,将变量分为局部变量和全局变量 1.局部变量 1> 定义:在函数内部定义的变量,称为局部变量.形式参数也属于局部变量. 2> 作用域:局部变量只在定 ...
- 向php文件中添加php.ini文件
向php文件中添加php.ini文件 默认情况下,php是没有php.ini配置文件的,必须手工添加php.ini文件 在php安装目录中,复制php.ini文件到/usr/local/php/lib ...
- 《锋利的jQuery》心得笔记--Three Sections
第六章 1. JavaScript的Ajax的实现步骤: 1) 定义一个函数用来异步获取信息 function Ajax(){ } 2) 声明: var xmlH ...
- struts2中访问servlet API
Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...
- 去掉影响效率的where 1=1
最近看了篇文章,觉得挺有道理.实际项目中,我们进行sql条件过滤,我们不能确定是不是有条件.也不能确定条件的个数.大多数人会先把sql语句组装为: 这样,如果有其他过滤条件直接加上“and 其他条件” ...
- UIPickerView基本用法
#import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController : UIVie ...
- 网络A、B、C类IP地址的区别
学网络之前得先要明白几个概念:(起初我也不怎么知道后来就慢慢会了) 1字节=8位,1位就是1个数字,所以1字节等于8个数字. 2的8次方,和二进制11111111转换为十进制并不一回事. 0-127是 ...
- Eclispe使用Maven添加官方库的jar包
先到百度或google搜索maven仓库,在仓库中搜索需要的jar包,如poi.jar. 搜索到之后找到需要的jar包,找到这里