poj 1738 取石子游戏 区间dpgarsiawachs算法
来源:互联网 发布:凶暴的男人知乎 编辑:程序博客网 时间:2024/05/01 14:08
http://poj.org/problem?id=1738
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.
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.
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
110033 4 341 1 1 10
Sample Output
0178
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<cmath>#include<map>#include<queue>using namespace std;const int N=50005;int stone[N],ans,j;void add(int k){ int tem=stone[k]+stone[k-1]; ans+=tem; for(int i=k ; i<j-1; i++) { stone[i]=stone[i+1]; } j--; int m; for(m=k-1 ; m>0 && stone[m-1]<tem ; m--) { stone[m]=stone[m-1]; } stone[m]=tem; while(m>=2 && stone[m-2]<=stone[m]) { int d=j-m; add(m-1); m=j-d; }}int main(){ int n; while(cin>>n && n) { for(int i=0; i<n; i++) cin>>stone[i]; j=1;ans=0; for(int i=1; i<n; i++) { stone[j++]=stone[i]; while(j>=3 && stone[j-3]<=stone[j-1]) add(j-2); } while(j>1) add(j-1); cout<<ans<<endl; } return 0;}
算法解释:http://fanhq666.blog.163.com/blog/static/81943426201062865551410/
0 0
- poj 1738 取石子游戏 区间dpgarsiawachs算法
- POJ 取石子游戏
- poj 1067 取石子游戏
- poj 1067 取石子游戏
- POJ 1067 取石子游戏
- POJ-1067:取石子游戏
- poj 1067 取石子游戏
- poj-1067 取石子游戏
- POJ 1067 取石子游戏
- POJ-1067-取石子游戏
- POJ 1067 取石子游戏
- POJ 1067 取石子游戏
- POJ-1067 取石子游戏
- POJ 1067 取石子游戏
- poj 1067 取石子游戏
- POJ 1067取石子游戏
- poj 1067 取石子游戏
- POJ 1067 取石子游戏
- 安卓实训第六天:首先使用AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取
- 第一次实习面试总结
- hdu 4824 Disk Schedule双调欧几里得旅行商问题(dp)
- 线程的控制和常用的方法
- 三十六计之28计敌战篇—笑里藏刀
- poj 1738 取石子游戏 区间dpgarsiawachs算法
- hdu 4832 Chess(计数+dp)
- Servlet开发基础笔记
- Java实现HangMan自动解题程序---初涉AI
- 第十三周 1
- Java编写 函数数组(键盘输入n个数) 冒泡排序
- jsp与servlet之间的参数传递
- 第十三周2
- 黑马程序员--学习笔记(GUI)