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 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
代码
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <sstream> 10 #include <queue> 11 #include <typeinfo> 12 #include <fstream> 13 #include <map> 14 #include <stack> 15 typedef __int64 ll; 16 using namespace std; 17 inline ll read() 18 { 19 ll x=0,f=1; 20 char ch=getchar(); 21 while(ch<'0'||ch>'9') 22 { 23 if(ch=='-')f=-1; 24 ch=getchar(); 25 } 26 while(ch>='0'&&ch<='9') 27 { 28 x=x*10+ch-'0'; 29 ch=getchar(); 30 } 31 return x*f; 32 } 33 //************************************************************************************** 34 ll t=1000000; 35 int n; 36 ll sum[200000]; 37 ll a[200000]; 38 int main() 39 { 40 41 scanf("%d",&n); 42 for(int i=1; i<=n; i++) 43 { 44 ll h,m,s; 45 cin>>h>>m>>s; 46 a[i]=s+m*t+t*t*h; 47 } 48 sort(a+1,a+n+1); 49 for(int i=1;i<=n;i++) 50 sum[i]=sum[i-1]+a[i]; 51 ll tt=12*t*t; 52 ll ans=tt*1000000;//此处无穷大就好了 53 54 for(int i=n;i>=1;i--) 55 { 56 ll xx=(a[i]*(i-1)-sum[i-1]+(a[i]+tt)*(n-i)-(sum[n]-sum[i])); 57 ans=min(xx,ans); 58 } 59 printf("%I64d %I64d %I64d\n",(ans/t)/t,(ans/t)%t,ans%t); 60 return 0; 61 }