您的位置:首页 > 其它

Codeforces Gym 100637A A. Nano alarm-clocks 前缀和处理

2015-07-24 23:00 316 查看

A. Nano alarm-clocks

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100637/problem/A

Description

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: