This is the 2011 Baidu star of a problem. The practice of this problem is to find the law, the law to find the right, the code is very water. I did not find the law at the beginning, and later, I was reminded that the following rules: for each developer, the separation of all its applications and must be its first application and the distance from the last application
For example, 15 8 5 2 This set of data, there are many kinds of methods, of which three kinds of methods are as follows
1 2 3 1 2 3 1 2 1 2 1 2 1 1 1
0 0 0 3 3 3 3 3 2 2 2 2 2 1 1
This method, the separation of three applications and the 14,10,3, the sum of 27
1 1 1 1 2 2 3 2 3 2 2 1 1 1 1
0 1 1 1 0 1 0 2 2 2 1 8 1 1 1
This method, the separation of three applications and the 14,6,2, the sum of 22
1 2 3 1 1 1 1 1 1 2 2 2 3 2 1
0 0 0 3 1 1 1 1 1 8 1 1 10 2 6
This method, the separation of three applications and the 14,12,10, the sum of 36
It is not difficult to see the correctness of the above laws.
With this rule, the separation of all the icons and the maximum is what makes each developer's separation and maximum. It is not difficult to think that if a developer has only one application, then its degree of separation and must be zero, otherwise (that is, the application number is greater than or equal to 2), to make the application of the developer of the separation and the largest is to make its first application and the last application distance maximum. In this way, for all developers with a number greater than or equal to 2, the status is exactly the same, so you only need to remove two apps from each of these developers in turn to each of the icons at each end. The calculation is very simple, my code is as follows, in fact, you can also organize a more concise formula:
/** Author:ben*/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<map>#include<stack>#include<string>#include<vector>#include<deque>#include<list>#include<functional>#include<numeric>#include<cctype>using namespacestd; #ifdef on_local_debug#else#endiftypedefLong LongLL;intMain () {#ifdef on_local_debug freopen ("data.in","R", stdin);#endif intm, N, D, A; while(SCANF ("%d%d", &n, &m) = =2) {a= N-1; LL ans=0; for(inti =0; I < m; i++) {scanf ("%d", &d); if(d >=2) {ans+=A; A-=2; }} printf ("%i64d\n", ans); } return 0;}
bjfu1097 icon Arrangement