Codeforces Gym 100637A A. Nano alarm-clocks 前缀和处理
2015-07-24 23:00
316 查看
A. Nano alarm-clocks
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/gym/100637/problem/ADescription
An old watchmaker has n stopped nano alarm-clocks numbered with integers from 1 to n. Nano alarm-clocks count time in hours, and in one hour there are million minutes, each minute lasting a million seconds. In order to repair them all the watchmaker should synchronize the time on all nano alarm-clocks. In order to do this he moves clock hands a certain time forward (may be zero time). Let’s name this time shift a transfer time.Your task is to calculate the minimal total transfer time required for all nano alarm-clocks to show the same time.
Input
The first line contains a single integer n — the number of nano alarm-clocks (2 ≤ n ≤ 105). In each i-th of the next n lines the time h, m,s, shown on the i-th clock. Integers h, m and s show the number of hours, minutes and seconds respectively. (0 ≤ h < 12, 0 ≤ m < 106,0 ≤ s < 106).
Output
Output three integers separated with spaces h, m and s — total minimal transfer time, where h, m and s — number of hours, minutes and seconds respectively (0 ≤ m < 106, 0 ≤ s < 106).
Sample Input
2
10 0 0
3 0 0
Sample Output
5 0 0
HINT
题意给你n个时钟,只可向前拨 问你总计拨多少时间,可以使得所有表的时间一样
题解:
排序+维护前缀和,暴力出最小的就OK
代码
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef __int64 ll; using namespace std; inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //************************************************************************************** ll t=1000000; int n; ll sum[200000]; ll a[200000]; int main() { scanf("%d",&n); for(int i=1; i<=n; i++) { ll h,m,s; cin>>h>>m>>s; a[i]=s+m*t+t*t*h; } sort(a+1,a+n+1); for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; ll tt=12*t*t; ll ans=tt*1000000;//此处无穷大就好了 for(int i=n;i>=1;i--) { ll xx=(a[i]*(i-1)-sum[i-1]+(a[i]+tt)*(n-i)-(sum -sum[i])); ans=min(xx,ans); } printf("%I64d %I64d %I64d\n",(ans/t)/t,(ans/t)%t,ans%t); return 0; }
View Code
相关文章推荐
- hdu 1728 bfs **
- 十六、C# 常用集合类及构建自定义集合(使用迭代器)
- AD技巧之通过Excel快速制作原理图库
- 股票学习(技术指标)
- Ognl的讲解
- 使用Lua 局部变量来优化性能,同时比较局部变量和全局变量
- BRISK特征提取算法
- 打牌~~~
- hdu 1068 poj 1466 最大独立集
- 数据结构链表——双链表
- Struts2学习记录-Value Stack(值栈)和OGNL表达式
- 从唯一实例谈静态成员
- 数字反转(升级版)
- 2015.7.24 改正后的稍简陋通讯录
- gadget编码与单元测试和service编写示例
- 计数排序、桶排序和基数排序
- java 之CPU~CPU缓存(转)
- bzoj2654
- java 选择排序
- 最新期货交易时间表