topic, given n clocks, required to tune them into the same time. To find the minimum number of steps
Idea: There must be a clock as standard, to find this clock, how to find it? There is no other convenient way to brute force enumeration. How can you quickly count the time that other clocks go to this clock after enumeration? First of all, if we convert time into numbers, that should be a good point. Now the problem is to give the N number, enumerate the smallest number of steps, and make n numbers the same.
For example, we change the time to 1, 3, 5, 7, 9 and so on. (When the output is changed to a time according to the weight value)
Now enumeration 5, preprocessing a prefix and sum[i], then the number in the 5 front of the total change step can be quickly calculated. I*a[i]-sum[i]
The back, because can not drop down, can only increase. 7 to 5 requires +8 actually 10-(7-5). So the value of the second half is (n-i) *10-(sum[n]-sum[i]-(n-i) *a[i]);
So it can be solved by O (n). Using the prefix and
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 1e5+ -; LL A[MAXN];ConstLL hh =1e12;ConstLL mm =1e6; LL SUM[MAXN];voidWork () {intN; CIN>>N; for(intI=1; i<=n;++i) {LL u,v,w; CIN>>u>>v>>W; A[i]=u*hh+v*mm+W; } sort (A+1, A +1+N); for(intI=1; i<=n;++i) {Sum[i]= sum[i-1] +A[i]; } LL ans=-1; for(intI=1; i<=n;++i) {LL T= i*a[i]-Sum[i]; T+ = (n-i) *hh* A-(Sum[n]-sum[i]-((n-i) *a[i])); if(ans==-1) ans=T; Elseans=min (ans,t); //cout<<t<< "" <<i<<endl;} LL T= ans/hh; Ans-= t*hh; LL T1= ans/mm; Ans-= t1*mm; cout<<t<<" "<<t1<<" "<<ans<<Endl; return ;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endifWork (); return 0;}
View Code
Gym 100633G Nano Alarm-clocks