UVa 10137 & ZOJ 1874 The Trip
题目大意:n个学生去旅行,旅行中每个学生先垫付,最后平摊所有费用,多退少补,并且支出差距控制在1分钱以内,求最小的交易金额。
@2013-8-16
以前在zoj做过,把原来的代码直接提交了,虽然AC了,可是记得原来有问题,再一看确实感觉有问题,竟然AC了...然后就自己重写,写完之后总是WA,就放下了,过一段时间在看,再改,提交,仍是WA,太打击人了,不算难的一道题把我虐成这样...今天看World of Seven上的解法时猛然醒悟,自己一直在纠结精度方面的问题,却没发现在向下截取aver时转换成int后直接除100,变成了整数的除法了...只能无语了...
自己的思路就是求平均数后向下截取,然后用 总金额-平均数*n 得到少的金额,对每人的花费进行排序,少的金额就通过让后面的人多拿一美分补上。
#include <stdio.h>
#include <stdlib.h>
#define MAXN 1010 int cmp(const void *_a, const void *_b)
{
double *a = (double*)_a;
double *b = (double*)_b;
return *a - *b;
} int main(void)
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n, i, p;
double aver, sum, exchange;
double cost[MAXN];
while (scanf("%d", &n) != EOF)
{
if (n == ) break;
sum = ;
for (i = ; i < n; i++)
{
scanf("%lf", &cost[i]);
sum += cost[i];
}
aver = sum / n;
aver = (int)(aver*)/100.0; /* floor the aver */
exchange = ;
p = (int)((sum-aver*n)/0.01+0.1);
qsort(cost, n, sizeof(double), cmp);
for (i = n-; i >= n-p; i--)
if (cost[i] > aver+0.01)
exchange += cost[i]-(aver+0.01);
for (i = n-p-; i >= ; i--)
if (cost[i] > aver)
exchange += cost[i]-aver;
printf("$%.2lf\n", exchange);
}
return ;
}
这个题原来留了好几份代码,现在看看也能看出一些东西,从最初的用c自己用选择排序进行排序到后来用qsort函数排序,到现在用c++的sort,以及代码风格的一些小小改进,发现自己也不是毫无变化了:D,不过长进的也就这些罢了,算法方面还是没什么长进 -_-||
网上流行另一个版本,World of Seven 上如下描述:
The problem with this problem may be related to precision error. Here is solution by Neilor:
  double highx = (int)((total/n+0.0099)*100);
	  double lowx = (int)((total/n)*100);
	  highx /= 100;
	  lowx /= 100;
	  Where total is the total sum of the money and n is the number of students.
  Then, test each student money if is > than highx or < than lowx, accumulate
	(student[i]-highx) or (lowx-students[i]), respectively.
	  Then, output the variable that have the bigger value.
不理解为什么可以这样做,也许某天就顿悟了呢?:D
UVa 10137 & ZOJ 1874 The Trip的更多相关文章
- UVa 706 & ZOJ 1146 LC-Display
		
题目大意:给你一个数字n和字体大小s,输出数字的液晶显示.直接模拟,代码如下: #include <stdio.h> void draw(int n,int s,int row) { in ...
 - zoj 1874 水题,输出格式大坑
		
Primary Arithmetic Time Limit: 2 Seconds Memory Limit: 65536 KB Children are taught to add mult ...
 - 详解OJ(Online Judge)中PHP代码的提交方法及要点【举例:ZOJ 1001 (A + B Problem)】
		
详解OJ(Online Judge)中PHP代码的提交方法及要点 Introduction of How to submit PHP code to Online Judge Systems Int ...
 - 通过vjudge刷Uva的题目(解决Uva网站打开慢的问题)
		
最近在跟着算法竞赛入门经典刷题,发现Uva网站打开超级慢,进个主页面都需要好几秒.后来发现可以通过vjudge网站刷Uva的题目,很是方便,在这mark一下,顺便做一下推荐. vjudge网址:htt ...
 - ZOJ 1141:Closest Common Ancestors(LCA)
		
Closest Common Ancestors Time Limit: 10 Seconds Memory Limit: 32768 KB Write a program that tak ...
 - The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1
		
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
 - POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
		
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
 - POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)
		
POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...
 - UVA 11100  The Trip, 2007 (贪心)
		
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
 
随机推荐
- android studio sexy editor性感编辑器设置
			
sexy editor下载地址:http://download.csdn.net/detail/yy1300326388/9166223 我自己也有上传CSDN资源 rainyday0524@163. ...
 - location对象的属性和方法应用(解析URL)
			
本文将与大家分享下location对象使用属性和方法来解析URL的实例,感兴趣的朋友可以参考下,希望对你有所帮助 location对象提供了很多属性和方法用来解析URL. 复制代码代码如下: &l ...
 - jq实现点击按钮后倒计时,多用于手机验证
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - discuz 添加板块失败解决办法
			
最近把服务器环境升了下级,发现discuz后台添加栏目添加不了了,数据库没变,源代码没变,就突然添加不了了.刚开始添加1个板块成功了,再添加就怎么也添不进去了.只是页面刷新了一下,啥提示没有. 经过一 ...
 - openwrt串口的使用
			
从 RT5350 的芯片手册上可以得知, RT5350 一共有两个串口, 分别为 UART Lite. UART Full, UART Lite 就是我们惯称为的串口 1,作为系统调试串口,通过这个串 ...
 - 【WiFi密码破解详细图文教程】ZOL仅此一份 详细介绍从CDlinux U盘启动到设置扫描破解-破解软件论坛-ZOL中关村在线
			
body { font-family: Microsoft YaHei UI,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-ser ...
 - 定时且周期性的任务研究II--ScheduledThreadPoolExecutor
			
http://victorzhzh.iteye.com/blog/1011635 上一篇中我们看到了Timer的不足之处,本篇我们将围绕这些不足之处看看ScheduledThreadPoolExecu ...
 - 安卓图表引擎AChartEngine(六) - 框架源码结构图
			
包结构: org.achartengine: org.achartengine.model: org.achartengine.renderer: org.achartengine.tools: 安卓 ...
 - RS485通讯协议的应用 (转)
			
源:http://blog.chinaunix.net/uid-26921272-id-3506640.html RS485缺点: RS485总线是一种常规的通信总线,它不能够做总线的自动仲裁,也就是 ...
 - javascript 中的闭包
			
在 javascript 中,函数可以当做参数传递,也可以当做返回值返回. 当一个函数内部返回值为一个函数时, 就形成了闭包.(闭包里面的 this 问题) 如下面代码 Function.protot ...