[Median properties] energy transmission

Title limit
1500 ms 128 M

Title Description
adventure Hatchet galaxies, Jim and his Rangers suffered a swarm of attack. Jim outstanding ability to command his troops in no serious
under the feet of the situation remains the fight against the repeated attacks of the Swarm. But at the same time a part of Jim soldiers fighting service but not enough energy to support them following the
continued fighting. For more fire suppression, Jim decided to split the energy of all the soldiers, so that everyone can participate in combat.
There are known Jim n marines, Jim will be surrounded by the n soldier a circle, the sum of energy of all the soldiers is a multiple of n, and each transmit
only an energy transmission unit. Worse, the length of the charging cable is fixed, which means, you can only make separated by k-bit
two energy exchanges.
Hugh Hyperion number requires a very long time to fall from orbit, Jim want to know the minimum number of transfers in the fastest time.

The input format
of the first two acts of positive integers n, k n represents the total marines. (n <= 500000, k < = n)
of the second row comprising n positive integer val [1] -val [n] , representing each Marines energy (val [i] <= 1e8 )
ensure that the final result can be used 64-bit integer stored
output format
frequency and output transmission minimum. Data are divided equally guaranteed to all energy.

Data range
for 25% of the data is guaranteed n <= 10;
for 50% of the data to ensure that n <= 2000
for a 100% guarantee of data n <= 500000

Sample input
. 4. 1
. 1. 3. 9. 7
Output Sample
6

Sample explained
example, there are four energy respectively per 1397 so that two adjacent an energy exchange
[1,3,9,7] → [5,3,5,7] (four times)
[5,3,5,7] → [5,5,5,5] (twice)
the answer is six times

The entire ring is divided into a plurality of rings to model individual candy classical transfer

https://blog.csdn.net/weixin_34238642/article/details/94200423

I have not written this model, then all of a sudden think of another similar classics, I played on the success of that question my fantasy. . . . .

Hand measurement found where not quickly turn violent. . .

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int N=500005;
 5 int n,k,val[N]; bool vis[N];
 6 int m; long long ans,a[N],b[N],sum[N];
 7 inline long long hehe()
 8  {long long aver=0;
 9   for(int i=1;i<=m;i++) aver+=a[i];
10   aver/=m; 
11   
12   for(int i=1;i<m;i++) b[i]=aver-a[i+1],sum[i]=sum[i-1]+b[i];
13   
14   sum[m]=0;
15   sort(sum+1,sum+m+1); // 1--m
16   long long x=sum[(m+1)/2],re=0; 
17   for(int i=1;i<=m;i++) re+=abs(x-sum[i]);
18   
19   return re;
20  }
21 int main()
22  {    
23  scanf("%d%d",&n,&k);    
24  for(int i=1;i<=n;i++) scanf("%d",&val[i]);    
25      
26  for(int i=1;i<=n;i++)    
27   {if(vis[i]) continue;
28    m=0;
29    for(int j=i;;j=(j+k)%n+1)
30     {if(vis[j]) break;
31      vis[j]=1; a[++m]=val[j]; //printf("%lld ",a[m]);
32     }
33    //printf("\n"); 
34    ans+=hehe(); 
35   }    
36  printf("%lld",ans);    
37 return 0;    
38  }

 

Guess you like

Origin www.cnblogs.com/YuXiaoze/p/11827423.html