Naughty Stone Piles

Topic: http: //codeforces.com/problemset/problem/227/D

Meaning of the questions: n number of stones heap, each heap of stones have A i a , by combining (a pile of stones is about to moved to another pile of stones), all combined into a pile of stones, each combined cost of this pile of stones number, the k I under limitation, stones piled up each can be combined k times, it is necessary to move the stones after the k th stack to another stack, to find the final total combined minimum cost.

Solution to a problem: emmmmm, since it is to find a minimum cost, then the largest number of stones pile of stones is certainly not moving, put a small pile of stones k several times to move this pile of stones, so this move a k heap of stones, this k pile of stones in each of them is smaller than k heap of stones incorporated into their body, that this k * k heap moved twice, and so on:

k 1 times

k * k 2 times

k * k * k 3 times

k * k * k * k 4 times

         .

         .       

         .

         .

That is, so the total cost of the minimum number of k * 1 * + k ^ 2 * 2 * number + k ^ 3 * 3 * Number .....

Of course, k is a need Laid sentence, the minimum heap moved n-1 times, the second smallest move the stack n-2 times, the maximum stack does not move, look at the specific code.

Note that most of the data are long long range.

 1 #include <map>
 2 #include <stack>
 3 #include <queue>
 4 #include <cmath>
 5 #include <string>
 6 #include <limits>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <cstdlib>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 #define Scc(c) scanf("%c",&c)
14 #define Scs(s) scanf("%s",s)
15 #define Sci(x) scanf("%d",&x)
16 #define Sci2(x, y) scanf("%d%d",&x,&y)
17 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
18 #define Scl(x) scanf("%I64d",&x)
19 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
20 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
21 #define Pri(x) printf("%d\n",x)
22 #define Prl(x) printf("%I64d\n",x)
23 #define Prc(c) printf("%c\n",c)
24 #define Prs(s) printf("%s\n",s)
25 #define For(i,x,y) for(int i=x;i<y;i++)
26 #define For_(i,x,y) for(int i=x;i<=y;i++)
27 #define FFor(i,x,y) for(int i=x;i>y;i--)
28 #define FFor_(i,x,y) for(int i=x;i>=y;i--)
29 #define Mem(f, x) memset(f,x,sizeof(f))
30 #define LL long long
31 #define ULL unsigned long long
32 #define MAXSIZE 100005
33 #define INF 0x3f3f3f3f
34 const int mod=1e9+7;
35 const double PI = acos(-1.0);
36 
37 using namespace std;
38 int cmp(LL a,LL b)
39 {
40     return a<b;
41 }
42 int main()
43 {
44 
45     LL n;
46     Scl(n);
47     int i;
48     LL a[MAXSIZE]= {0};
49     for (i=1; i<=n; i++)
50         Scl(a[i]);
51     sort(a+1,a+1+n);
52     LL  tmp=0;
53     For_(i,1,n-1 ) // Note that the order of these two for loops 
54 is      tmp = A + [I] * (N- I);
 55      For_ (I, 1 , n-)
 56 is      A [I] = A + [I- 1 ]; // used to calculate the power of x k times every stack stones are combined is the same, i.e., (a [m] -a [MK]) * CNT, 
57 is      int Q;
 58      Sci (Q);
 59      the while ( q - )
 60      {
 61 is          LL ANS = 0 ;
 62 is          LL K;
 63 is          Scl (K);
 64          int T = K;
 65          LL = CNT . 1 , m = N- . 1;
 66          IF (! = K . 1 )
 67          {
 68              the while (m> = k) // when k is greater than equal to n, is not executed, direct execution ans + = cnt * a [m ] ;, i.e. the minimum cost is a front n- 1 stack and the number of stones, i.e. A * 1 [m] 
69              {
 70                  ANS + = ([m] -a A [MK]) * CNT;
 71 is                  CNT ++ ;
 72                  M- = K;
 73 is                  K = K * T; // change k is kk ^ 2 k ^ 3 k ^ 4 ... every time multiplied by the size of the initial k, the beginning written k * = k, wa one afternoon, I was too hard QAQ 
74              }
 75              ANS + = * CNT A [m];
 76          }
 77          the else 
78             = ANS tmp;
 79          the printf ( " % I64d " , ANS); // also output this mystery. 
80      }
 81      return  0 ;
 82 }
View Code

 Ah, had not a problem, he just took me one day, really. . . . . . Many places have worked well. Ugh.

Guess you like

Origin www.cnblogs.com/hbhdhd/p/11413957.html