hdu 5461 Largest Point

Thinking about it:

  对于式子 a * ti * ti + b * tj,可以看作时有两部分构成 a * ti * ti 和 b * tj,如果整个式子要最大,则要求这两部分都要尽量大。那么再读入数据 t 时,那么就可以构造两个数组,一个存储a * ti * ti ,另一个存储 b * tj。在选取时就产生了两种方案:

  1. 先选取 a * ti * ti 最大的,接着再 i != j 的基础上, 选择 b * tj 最大的。

  2. 先选取 b * tj 最大的,接着再 i != j 的基础上, 选择 a * ti * ti 最大的。

可以算出两种情况下的值,比较大小三即可。

PS:

  最开始我的办法是求出 t 中的 最大值,绝对值最大值,最小值,绝对值最小值,根据a,b正负等情况分类讨论。就运算时间上,这种方法一般会更快点,不过要把情况仔细。

Code:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int MAXN = 5 * 10e6 +50;
LL N, a, b;
LL sub_a[MAXN], sub_b[MAXN];

LL way(LL first[], LL second[]) {
    LL ans = (LL)MAXN * MAXN * -1;
    int pos;
    for (int i = 0; i < N; ++i) {
        if (first[i] > ans) {
            ans = first[i];
            pos = i;
        }
    }
    LL maxC = (LL)MAXN * MAXN * -1;
    for (int i = 0; i < N; ++i) {
        if (second[i] > maxC && i != pos) {
            maxC = second[i];
        }
    }
    return ans + maxC;
}

int Case = 0;
void work() {
    cin >> N >> a >> b;
    LL tmp;
    for (int i = 0; i < N; ++i) {
        cin >> tmp;
        sub_a[i] = tmp * tmp * a;
        sub_b[i] = tmp * b;
    }
    cout << "Case #" << (++Case) << ": " << max(way(sub_a, sub_b), way(sub_a, sub_b)) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T --) {
        work();
    }
    return 0;
}

  

时间: 2024-07-28 21:13:40

hdu 5461 Largest Point的相关文章

hdu 5461 Largest Point 暴力

Largest Point Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5461 Description Given the sequence A with n integers t1,t2,?,tn. Given the integral coefficients a and b. The fact that select two elements ti and tj

HDU 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

1506题意:给你连续的直方图(底边边长为1),求连续的矩阵面积. 对每个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <sta

HDU 2870 Largest Submatrix(DP)

题意  求最大相同字符子矩阵  其中一些字符可以转换 其实就是HDU1505 1506的加强版  但是分了a,b,c三种情况  看哪次得到的面积最大 对于某一个情况  可以把该字符和可以转换为该字符的位置赋值0 其它位置赋值1 这样就转化成了求最大全0矩阵的问题了 对于转换后矩阵中的每个点 看他向上有多少个连续0 把这个值存在h数组中 再用l数组和r数组记录h连续大于等于该位置的最左边位置和最右位置 这样包含(i,j)点的最大矩阵面积就是(r[i][j]-l[i][j]+1)*h[i][j] 面

Largest Point hdu 5461

http://acm.hdu.edu.cn/showproblem.php?pid=5461 分析:一开始很撒很撒的分了很多种情况去写了,后来发现可以先储存a*num*num和b*num的值,然后再判断最大值.. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm&

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

hdu 2870 Largest Submatrix

Largest Submatrix http://acm.hdu.edu.cn/showproblem.php?pid=2870 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change '

HDU 2870 Largest Submatrix (单调栈)

http://acm.hdu.edu.cn/showproblem.php?pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1569    Accepted Submission(s): 748 Problem Description Now here is a matrix wit

hdu 1506 Largest Rectangle in a Histogram 构造

题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rec