zoukankan      html  css  js  c++  java
  • 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
    ",dp[n]);
    41     }
    42     return 0;
    43 }
    View Code
     
  • 相关阅读:
    [已读]Sass与Compass实战
    微信小程序图片变形解决方法
    ES6基础教程一 学习笔记
    微信小程序图片放大预览
    Element ui表格展示多张图片问题
    Element ui表格展示图片问题
    使用element ui 日期选择器获取值后的格式问题
    在vue2.0中使用sass
    微信小程序左滑删除功能
    微信小程序用setData修改数组或对象中的一个属性值
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4458214.html
Copyright © 2011-2022 走看看