Codeforces Gym 100637A A. Nano alarm-clocks 前缀和
2015-07-24 18:18
387 查看
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.
[b]Input[/b]
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).
[b]Output[/b]
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).
[b]Sample Input[/b]
2
10 0 0
3 0 0
[b]Sample Output[/b]
5 0 0
HINT
[b]题意[/b]
给你n个时钟,问你总计转多少时间,可以使得所有表的时间一样
注意,只能往前拨
[b]题解:[/b]
先排序,然后维护前缀和,对于每一个在他前面的表,可能时间只能转到和他的时间一样的时候
在他后面的表,就转到他的时间+12h就好了
然后跑一遍
[b]代码[/b]
#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 long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) const int maxn=202501; #define mod 1000000007 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; 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; } //************************************************************************************** unsigned long long ti[maxn]; unsigned long long sum[maxn]; int main() { int n=read(); for(int i=1;i<=n;i++) { unsigned long long x=read(),y=read(),z=read(); ti[i]=z+y*1000000LL+x*1000000LL*1000000LL; } sort(ti+1,ti+1+n); for(int i=1;i<=n;i++) sum[i]=sum[i-1]+ti[i]; unsigned long long ans=0; for(int i=1;i<=n;i++) { unsigned long long num=i*ti[i]-sum[i]; num+=(12LL*1000000LL*1000000LL+ti[i])*(n-i)-sum +sum[i]; if(i==1) ans=num; else ans=min(ans,num); } unsigned long long hour=1000000LL*1000000LL; unsigned long long h=ans/(1000000LL*1000000LL); unsigned long long m=(ans-h*(1000000LL*1000000LL))/1000000LL; unsigned long long s=ans-h*1000000LL*1000000LL-m*1000000LL; cout<<h<<" "<<m<<" "<<s<<endl; }
相关文章推荐
- 小米手机 的一些问题
- git常用命令和问题
- DNS VIEW 主从搭建
- Unity中的优化技术
- 虚幻4:C++ Actor增加Component
- HTML5 INPUT新增属性
- 安卓圆角按键选择器
- mysql 触发器学习
- 暑假集训——贪心专题——E题
- 【YII2学习笔记】20150724-3
- linux下修改系统日期与时间
- apple 官网 尺寸问题。
- thymeleaf 学习笔记-基础篇(中文教程)
- Poj 3694 Network (连通图缩点+LCA+并查集)
- HDOJ1286 1406 欧拉公式
- Day_01_猜拳游戏
- 安装Vmtools for linux
- Qt QTimer测试定时精度
- iOS学习之 plist文件的读写
- Menu