POJ 1738 An old Stone Game(石子合并 经典)
2017-07-26 15:59
501 查看
An old Stone Game
Description
There is an old stone game.At the beginning of the game the player picks n(1<=n<=50000) piles of stones in a line. The goal is to merge the stones in one pile observing the following rules:
At each step of the game,the player can merge two adjoining piles to
a new pile.The score is the number of stones in the new pile.
You are to write a program to determine the minimum of the total score.
Input
The
input contains several test cases. The first line of each test case
contains an integer n, denoting the number of piles. The following n
integers describe the number of stones in each pile at the beginning of
the game.
The last test case is followed by one zero.
Output
For each test case output the answer on a single line.You may assume the answer will not exceed 1000000000.
Sample Input
Sample Output
Time Limit: 5000MS | Memory Limit: 30000K | |
Total Submissions: 3672 | Accepted: 1035 |
There is an old stone game.At the beginning of the game the player picks n(1<=n<=50000) piles of stones in a line. The goal is to merge the stones in one pile observing the following rules:
At each step of the game,the player can merge two adjoining piles to
a new pile.The score is the number of stones in the new pile.
You are to write a program to determine the minimum of the total score.
Input
The
input contains several test cases. The first line of each test case
contains an integer n, denoting the number of piles. The following n
integers describe the number of stones in each pile at the beginning of
the game.
The last test case is followed by one zero.
Output
For each test case output the answer on a single line.You may assume the answer will not exceed 1000000000.
Sample Input
1 100 3 3 4 3 4 1 1 1 1 0
Sample Output
0 17 8
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define INF 0x3f3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; ll a[50006],ans,n,pos; void check(int x) { int i,j; int mx=a[x]+a[x-1]; ans+=mx; for(i=x;i<pos-1;i++) a[i]=a[i+1]; pos--; for(j=x-1;j>0 && a[j-1]<mx;j--) a[j]=a[j-1]; a[j]=mx; while(j>=2 && a[j-2]<=a[j]) { int y=pos-j;//j后面有多少个数,往前移; check(j-1); j=pos-y; } } int main() { while(scanf("%lld",&n)&& n) { for(int i=0;i<n;i++) { scanf("%lld",&a[i]); } pos=1;ans=0; for(int i=1;i<n;i++) { a[pos++]=a[i]; while(pos>=3 && a[pos-3]<=a[pos-1]) { check(pos-2); } } while(pos>1) check(pos-1); printf("%lld\n",ans); } return 0; }
相关文章推荐
- POJ 1738 An old Stone Game(石子合并)
- poj1738 An old Stone Game 石子合并(归并) GarsiaWachs算法
- POJ 1738 An old Stone Game 石子合并之GarsiaWachs算法
- poj 1738 An old Stone Game(区间dp 合并石子问题直线型)
- [GarsiaWachs算法] BZOJ 3229 [Sdoi2008]石子合并 & POJ 1738 An old Stone Game & 51Nod 1023 石子归并 V3
- POJ 1738:An old Stone Game 石子归并(GarsiaWachs算法)
- POJ 1738:An old Stone Game 石子归并(GarsiaWachs算法)
- poj1738 An old Stone Game 【GarsiaWachs算法】【动态规划】经典合并石子问题
- poj1738 an old stone game
- poj 1738 An old Stone Game(此题数小则可用区间DP,数较大用一维数组或者GarsiaWachs算法),待续
- poj1738 An old Stone Game 石子合并(归并) GarsiaWachs算法
- POJ 1738 An old Stone Game 笔记
- ZOJ1427 POJ1694 An Old Stone Game
- POJ 1694 An Old Stone Game【递归+排序】
- poj 1694 An Old Stone Game 树形dp
- POJ 1694 An Old Stone Game【递归+排序】
- poj 1694 An old stone game
- poj 1694 An Old Stone Game 树形dp
- POJ 1694 An Old Stone Game
- An Old Stone Game(poj1694模拟与排序)