DHUOJ 2017051002
来源:互联网 发布:定制化软件 编辑:程序博客网 时间:2024/06/05 08:22
DHU Club Festival
Description
During the past DHU Club Festival, XiaoTang got many bottles of drinks, but each was of a different taste. And they were not of the same concentration.
Sometimes it's just that confusing with too many choices. XiaoTang decided to mix all of them up to create an extreme unique taste. Please help him. Following is the rule of mixing.
Given the concentrations of each drink:
Hint: In this problem, we use integer division, eg:
Input
There are several test cases, each contains two lines.
The first line is
The second line contains
Output
For each test case, output the final concentration in a line.
Sample Input
22 311
Sample Output
21
Author: nature
思路:
n杯酒,每次都可以把若干杯匀到一起后再分摊,且会损失小数点后的精度。比如3,4平分之后变成了3,3,损失了1。
问,最多能每个杯子匀到多少。也就是使他们的损失最小。
本来以为是dp,但是仔细观察会发现其实是个数论问题。
当两个杯子匀的时候,分子为1则损失,分子为2就不会损失。
三个杯子的时候,分子为1、2会损失,分子为3不会损失。
四个杯子,分子为1、2、3会损失,分子为4不会损失
.......
也就是说,当两个杯子的匀的时候,最多会损失1,而当1个以上匀的时候,最少为1,因此肯定每次两个两个的匀会损失的最小。所以只需每次找最小的两个匀,再把均匀后的那个加进去继续和别的匀。有点类似于Huffman树的思想。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>#include<stack>using namespace std; int c[105]; int main() { int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++){ scanf("%d",&c[i]); } int sum=0; sort(c+1,c+n+1); int cnt=c[1]; for(int i=2;i<=n;i++){ cnt= (cnt+c[i])/2; } cout<<cnt<<endl; } return 0; }
- DHUOJ 2017051002
- DHUOJ 2017052401
- DHUOJ 2017052403
- DHUOJ 2016060702
- DHUOJ 2016060703
- 1.2 离散LTI系统的时域分析
- 逻辑运算符
- B. Vladik and Complicated Book
- 统计一个数字在排序数组中出现的次数
- 欢迎使用CSDN-markdown编辑器
- DHUOJ 2017051002
- 深入理解javascript原型和闭包(2)——函数和对象的关系
- 在Java se 环境中加入ee开发环境
- django request
- (145)光线追踪距离场柔和阴影
- 编程提升关键-编程思维
- 垃圾收集器与内存分配策略
- 正则表达式学习笔记
- 用datetime和pytz来转换时区