您的位置:首页 > 其它

【练习09】简单动态规划 1003 Super Jumping! Jumping! Jumping!

2013-07-14 17:57 435 查看
算法思路:动态规划

状态转移方程:d[i] = d[j] + a[i] ; j < i。分别尝试所有的j(j < i), 在内层循环中动态的更新d[i], 同时在内层的循环结束之后用d[i]更新max_score。

//模板开始
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<iomanip>
#include<string.h>
#define SZ(x) (int(x.size()))
using namespace std;

int toInt(string s){
istringstream sin(s);
int t;
sin>>t;
return t;
}
template<class T> string toString(T x){
ostringstream sout;
sout<<x;
return sout.str();
}
typedef long long int64;
int64 toInt64(string s){
istringstream sin(s);
int64 t;
sin>>t;
return t;
}
template<class T> T gcd(T a, T b){
if(a<0)
return gcd(-a, b);
if(b<0)
return gcd(a, -b);
return (b == 0)? a : gcd(b, a % b);
}
//模板结束(通用部分)

#define ifs cin

#define MAX_LEN 1005
int a[MAX_LEN];
int d[MAX_LEN];
int n;
int max_score;

void dp()
{
max_score = d[1];
for(int i = 2; i <= n; i++)
{
for(int j = 1; j < i; j++)
{
if(a[j] < a[i] && d[i] < d[j] + a[i])
{
d[i] = d[j] + a[i];
}
if(max_score < d[i])
{
max_score = d[i];
}
}
}
}

//【练习09】简单动态规划 1003 Super Jumping! Jumping! Jumping!

int main()
{
//ifstream ifs("shuju.txt", ios::in);
while(ifs>>n && n != 0)
{
memset(d, -1, sizeof(d));
memset(a, 0, sizeof(a));
for(int i = 1 ; i <= n; i++)
{
ifs>>a[i];
d[i] = a[i];
}
dp();
cout<<max_score<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: