[贪心] HDU 5461 Largest Point

题意

给定一个序列,求a*ti^ti + b *tj最大值。要求(i != j).

思路

由于题目数据非常大,所以估计时间复杂度只可能为O(n),所以我们可以将a*ti^ti + b *tj分成两部分(左右两部分)分别贪心求出最大值。因为i != j,所以必须记录一下下标,然后判断,如果下标不相等,max(左部分)+max(右部分),如果相等max(max(第二大的左部分)+max(右部分),max(左部分)+max(第二大的右部分)).

代码

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 5000000 + 10;

ll n, a, b;

struct AT {
    ll val;
    int indx;

    bool operator< ( const AT &rhs ) { return val < rhs.val; }
} at2[ maxn ], bt[ maxn ];

int main () {
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    int T;
    scanf ( "%d", &T );
    for ( int ks = 1; ks <= T; ++ks ) {
        //用 longlong输入否则WA
        scanf ( "%lld%lld%lld", &n, &a, &b );
        for ( int i = 0; i < n; ++i ) {
            ll t;
            scanf ( "%lld", &t );
            at2[ i ].val = ll ( a * t * t );
            bt[ i ].val = ll ( b * t );
            //必须要保存indx
            at2[ i ].indx = bt[ i ].indx = i;
        }

        sort ( at2, at2 + n );
        sort ( bt, bt + n );

        ll ans = at2[ n - 1 ].val + bt[ n - 1 ].val;
        //下表要保证不相等
        if ( at2[ n - 1 ].indx == bt[ n - 1 ].indx ) {
            ans = max ( at2[ n - 1 ].val + bt[ n - 2 ].val, at2[ n - 2 ].val + bt[ n - 1 ].val );
        }

        printf ( "Case #%d: %lld\n", ks, ans );
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值