zoukankan      html  css  js  c++  java
  • hdu 5280 Senior's Array

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=5280    

    Senior's Array

    Description

    One day, Xuejiejie gets an array $A$. Among all non-empty intervals of $A$, she wants to find the most beautiful one. She defines the beauty as the sum of the interval. The beauty of the interval---$[L,R]$ is calculated by this formula : beauty$(L,R) = A[L]+A[L+1]+……+A[R]$. The most beautiful interval is the one with maximum beauty.

    But as is known to all, Xuejiejie is used to pursuing perfection. She wants to get a more beautiful interval. So she asks Mini-Sun for help. Mini-Sun is a magician, but he is busy reviewing calculus. So he tells Xuejiejie that he can just help her change one value of the element of $A$ to $P$ . Xuejiejie plans to come to see him in tomorrow morning.

    Unluckily, Xuejiejie oversleeps. Now up to you to help her make the decision which one should be changed(You must change one element).

    Input

    In the first line there is an integer $T$, indicates the number of test cases.

    In each case, the first line contains two integers $n$ and $P$. $n$ means the number of elements of the array. $P$ means the value Mini-Sun can change to.

    The next line contains the original array.

    $1leq nleq 1000$, $-10^9leq A[i], Pleq 10^9$。

    Output

    For each test case, output one integer which means the most beautiful interval's beauty after your change.

    2
    3 5
    1 -1 2
    3 -2
    1 -1 2

    Sample Output

    8
    2

    数据才1000直接暴力。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<map>
     8 using std::max;
     9 using std::cin;
    10 using std::cout;
    11 using std::endl;
    12 using std::find;
    13 using std::sort;
    14 using std::map;
    15 using std::pair;
    16 using std::vector;
    17 using std::multimap;
    18 #define pb(e) push_back(e)
    19 #define sz(c) (int)(c).size()
    20 #define mp(a, b) make_pair(a, b)
    21 #define all(c) (c).begin(), (c).end()
    22 #define iter(c) decltype((c).begin())
    23 #define cls(arr,val) memset(arr,val,sizeof(arr))
    24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    25 #define rep(i, n) for (int i = 1; i <= (int)(n); i++)
    26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    27 const int N = 1010;
    28 const int INF = 0x7fffffff;
    29 typedef long long ll;
    30 ll p, ans, arr[N], sum[N];
    31 int main() {
    32 #ifdef LOCAL
    33     freopen("in.txt", "r", stdin);
    34     freopen("out.txt", "w+", stdout);
    35 #endif
    36     int t, n;
    37     scanf("%d", &t);
    38     while (t--) {
    39         ans =  -INF;
    40         scanf("%d %lld", &n, &p);
    41         rep(i, n) scanf("%lld", &arr[i]);
    42         rep(i, n) {
    43             ll tmp = arr[i]; arr[i] = p;
    44             rep(j, n) {
    45                 sum[j] = sum[j - 1] > 0 ? sum[j - 1] + arr[j] : arr[j];
    46                 ans = max(ans, sum[j]);
    47             }
    48             arr[i] = tmp;
    49         }
    50         printf("%lld
    ", ans);
    51     }
    52     return 0;
    53 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    [LeetCode] Rabbits in Forest 森林里的兔子
    [LeetCode] 780. Reaching Points 到达指定点
    [LeetCode] Swim in Rising Water 在上升的水中游泳
    [LeetCode] 777. Swap Adjacent in LR String 交换LR字符串中的相邻项
    [LeetCode] Split BST 分割二叉搜索树
    [LeetCode] Global and Local Inversions 全局与局部的倒置
    [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
    [LeetCode] Sliding Puzzle 滑动拼图
    [LeetCode] Basic Calculator IV 基本计算器之四
    [LeetCode] Jewels and Stones 珠宝和石头
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4641675.html
Copyright © 2011-2022 走看看