Recursive calculation of contributions (gym / 102028 / problem / I)

Distance

  There are n points horizontal line, left to right, labeled 1 to n. Of the i-th point (i + 1) is the distance from point ai for each n is an integer from 1 to k, requires you to choose exactly k different given point on a straight line between the selected point to maximize sum of the distances.

Entry

  The first line contains a positive integer T, represents the number of test cases, T <= 1000, the first line of each test case contains an integer n, the number of points, where 2 <n <10 ^ 5 comprises a second row ( n-1) th positive integers a1, a2 ... an-1, where 1 <= ai <= 10 ^ 4, we ensure that the sum of all n test samples does not exceed 10 ^ 6.

Export

  Rows containing n integers, the first integer i when the distance is maximum, and k = i. You should output exactly one space between each two adjacent numbers, and to avoid any trailing spaces in that row.

 

Resolution:

  It gives the title requirements on n points spaced from the n-1, taken at random to find k (1 <= k <= n) the sum of the maximum distance between the dots is the number of points. We can get the selected point when we should choose to increase the current point of maximum distance by a simple analysis, so we can simulate on paper manually selected point, after the simulation we can find, every election is a point when we choose the away on a point of maximum distance from the point, because in order to make the sum greater distance, so we choose a point becomes the leftmost selected, choose a rightmost (also reversed the order of the results agreed) this has been choose it.

  Actually we know how to choose the point, then we analysis the next step is how to calculate the distance and. We should find the point when a simulated election law, if at this time we chose two points of the left and right of the two points, then our next selected point increase in the distance is the same as the previous one (which like fixed around the endpoints, in which you choose any one place and end point to the left and right are left-right difference endpoint, painting a picture in the paper to know), but at a point you select (in this case, chose the three left, the right choose two), we can see from the last increase = increase of + this point and left his distance from the nearest point (the most recent is the new one from the left point) .

  With the above conclusions, we can find the complexity in O (n) is k = (1 ~ n) and the maximum distance.

program:

#include<bits/stdc++.h>
#define SISWS std::ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
using namespace std;
template<class T>inline void read(T &res)
{
    char c;T flag=1;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
    while((c=getchar())>='0 ' && C <= ' . 9 ' ) RES = RES * 10 + the C- ' 0 ' ; RES * = In Flag; 
} 
typedef Long  Long LL;
 const  int MAXN = 1E5 + 10 ;
 int n-;
 int A [MAXN]; / / storage location of n points 
LL ANS [MAXN]; // store answers 
void Solve () { 
    CIN >> n; 
    a [ . 1 ] = 0 ;
     for ( int I = 2 , AI; I <= n; I ++) { 
        CIN >> AI; 
        A [i] = A [i - . 1 ] + AI; // calculate the i-th point position 
    } 
    LL TEMP = 0 ; // store a dot will increase the number from 
    int L = . 1 , R & lt = n-;
     for ( int I = . 1 ; I <= n-; I ++ ) {
         IF (% I 2 ) { // selected leftmost point 
            ANS [I] ANS = [I - . 1 ] + TEMP; // answer is once increased from the distance + 
        } the else { 
            TEMP= A + [R & lt] - A [L]; // parse are explained 
            ANS [I] ANS = [I - . 1 ] + TEMP; // answer is once increased from the distance + 
            l ++, r - - ; 
        } 
    } 
    for ( int I = . 1 ; I <n-; I ++ ) { 
        COUT << ANS [I] << "  " ; 
    } 
    COUT << ANS [n-] << endl; 
} 
int main () { 
    SISWS; 
    int T; 
    CIN >> T;
     the while (T-- ) { 
        Solve (); 
    } 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/liuzuolin/p/11990891.html