标签:gray splay i+1 ase 元素 html 复杂 学习 技术
第1行:N(2 <= N <= 1000)
第2 - N + 1:N堆石子的数量(1 <= A[i] <= 10000)
输出最小合并代价
4
1
2
3
4
19
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <bitset> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <cmath> 10 #include <list> 11 #include <set> 12 #include <map> 13 #define rep(i,a,b) for(int i = a;i <= b;++ i) 14 #define per(i,a,b) for(int i = a;i >= b;-- i) 15 #define mem(a,b) memset((a),(b),sizeof((a))) 16 #define FIN freopen("in.txt","r",stdin) 17 #define FOUT freopen("out.txt","w",stdout) 18 #define IO ios_base::sync_with_stdio(0),cin.tie(0) 19 #define mid ((l+r)>>1) 20 #define ls (id<<1) 21 #define rs ((id<<1)|1) 22 #define N 2010 23 #define INF 0x3f3f3f3f 24 #define INFF ((1LL<<62)-1) 25 typedef long long LL; 26 using namespace std; 27 28 int n, m, T, c[N], s[N][N], sum[N], dp[N][N]; 29 int main() 30 {IO; 31 //FIN; 32 while(cin >> n){ 33 sum[0] = 0; 34 rep(i, 1, n){ 35 cin >> c[i]; 36 sum[i] = sum[i-1] + c[i]; 37 } 38 rep(i, 1, n){ 39 c[i+n] = c[i]; 40 sum[i+n] = sum[i+n-1]+c[i+n]; 41 } 42 43 mem(dp, 0); 44 per(i, n*2, 1){ 45 rep(j, i, n*2){ 46 if(i == j) { s[i][j] = i; continue; } 47 dp[i][j] = INF; 48 LL res = sum[j]-sum[i-1]; 49 rep(k, s[i][j-1], s[i+1][j]){ 50 if(dp[i][k]+dp[k+1][j]+res < dp[i][j]){ 51 dp[i][j] = dp[i][k]+dp[k+1][j]+res; 52 s[i][j] = k; 53 } 54 } 55 } 56 } 57 int ans = INF; 58 rep(i, 1, n) ans = min(ans, dp[i][i+n-1]); 59 cout << ans << endl; 60 } 61 return 0; 62 }
标签:gray splay i+1 ase 元素 html 复杂 学习 技术
原文地址:http://www.cnblogs.com/Jstyle-continue/p/6358352.html