码迷,mamicode.com
首页 > 其他好文 > 详细

fzuoj Problem 2177 ytaaa

时间:2015-04-26 20:53:57      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

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

 

 

fzuoj Problem 2177 ytaaa

标签:

原文地址:http://www.cnblogs.com/jeff-wgc/p/4458214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!