fzuoj Problem 2177 ytaaa

http://acm.fzu.edu.cn/problem.php?pid=2177

Problem 2177 ytaaa

Accept: 113    Submit: 265
Time Limit: 2000 mSec    Memory Limit : 32768 KB

 Problem Description

Ytaaa作为一名特工执行了无数困难的任务,这一次ytaaa收到命令,需要炸毁敌人的一个工厂,为此ytaaa需要制造一批炸弹以供使用。 Ytaaa使用的这种新型炸弹由若干个炸药组成,每个炸药都有它的威力值,而炸弹的威力值为组成这个炸弹的所有炸药的最大威力差的平方,即(max-min)^2,假设一个炸弹有5个炸药组成,威力分别为5 9 8 2 1,那么它的威力为(9-1)^2=64。现在在炸弹的制造流水线上已经有一行n个炸药,由于时间紧迫,ytaaa并没有时间改变它们的顺序,只能确定他们的分组。作为ytaaa的首席顾问,请你帮助ytaaa确定炸药的分组,使制造出的炸弹拥有最大的威力和。

 Input

输入由多组数据组成。第一行为一个正整数n(n<=1000),第二行为n个数,第i个数a[i]为第i个炸药的威力值(0<=a[i]<=1000)。

 Output

对于给定的输入,输出一行一个数,为所有炸弹的最大威力和。

 Sample Input

6 5 9 8 2 1 6

 Sample Output

77
 
 
分析:
 
一开始没有看懂题, 以为样例错啦(服了我啦),后来知道是分开几组计算的。
 动规可解:dp[i]表示前i个炸药能组成的最大威力和。
状态转移方程:dp[i]=max(dp[i],dp[j-1]+(a[i]-a[j])^2)。
 
AC代码:
 
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <stdlib.h>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <vector>
15 #pragma comment(linker, "/STACK:1024000000,1024000000")
16 #pragma warning(disable:4786)
17 
18 using namespace std;
19 
20 const int INF = 0x3f3f3f3f;
21 const int MAX = 1000 + 10;
22 const double eps = 1e-8;
23 const double PI = acos(-1.0);
24 
25 int dp[MAX];
26 int a[MAX];
27 
28 int main()
29 {
30     int n;
31     while(~scanf("%d",&n))
32     {
33         int i , j;
34         for(i = 1;i <= n;i ++)
35             scanf("%d",&a[i]);
36         memset(dp , 0 , sizeof(dp));
37         for(i = 1;i <= n;i ++)
38             for(j = i;j >= 1;j --)
39 dp[i] = max(dp[i] , dp[j - 1] + (a[i] - a[j]) * (a[i] - a[j]));
40         printf("%d\n",dp[n]);
41     }
42     return 0;
43 }
View Code

 

 

转载于:https://www.cnblogs.com/jeff-wgc/p/4458214.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值