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

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 hm,s, shown on the i-th clock. Integers hm 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 hm and s — total minimal transfer time, where hm 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个时钟,问你总计转多少时间,可以使得所有表的时间一样

注意,只能往前拨

题解:

先排序,然后维护前缀和,对于每一个在他前面的表,可能时间只能转到和他的时间一样的时候

在他后面的表,就转到他的时间+12h就好了

然后跑一遍

代码

#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[n]+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;
}

 

posted @ 2015-07-24 18:18  qscqesze  阅读(557)  评论(0编辑  收藏  举报