HDU 5461 Largest Point (水题)

本文介绍了如何解决HDU 5461题目——Largest Point,这是一个简单的算法问题,通过开两个数组进行维护来求解。
摘要由CSDN通过智能技术生成
Problem 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  of  A  and  ij  to maximize the value of  at2i+btj , becomes the largest point.
 

Input
An positive integer  T , indicating there are  T  test cases.
For each test case, the first line contains three integers corresponding to  n (2n5×106), a (0|a|106)  and  b (0|b|106) . The second line contains  n integers  t1,t2,,tn  where  0|ti|106  for  1in .

The sum of  n  for all cases would not be larger than  5×106 .
 

Output
The output contains exactly  T  lines.
For each test case, you should output the maximum value of  at2i+btj .
 

Sample Input
  
  
2 3 2 1 1 2 3 5 -1 0 -3 -3 0 3 3
 

Sample Output
  
  
Case #1: 20 Case #2: 0
 



开两个数组维护一下


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=5e6+100;
struct node
{
	__int64 num;
	int index;
} aa[maxn],bb[maxn];

bool cmp(node x,node y)
{
	if(x.num<y.num) return true;
	if(x.num==y.num&&x.index<y.index) return true;
	return false;
}

int main()
{
	int i,j,t,n,a,b;
	scanf("%d",&t);
	__int64 ans,t1,t2;
	for (int z=1;z<=t;z++) {
		scanf("%d%d%d",&n,&a,&b);
		for(i=0;i<n;i++) {
			scanf("%I64d",&bb[i].num);
			bb[i].index=i;
			aa[i].num=bb[i].num*bb[i].num;
			aa[i].index=i;
		}
		sort(aa,aa+n,cmp);
		sort(bb,bb+n,cmp);
		if(a==0) ans=max(b*bb[0].num,b*bb[n-1].num);
		else if(b==0) ans=max(a*aa[0].num,a*aa[n-1].num);
		else if(a<0) {
			if(b<0) {
				if(aa[0].index==bb[0].index) {
					 t1=a*aa[1].num+b*bb[0].num;
					 t2=a*aa[0].num+b*bb[1].num;
					ans=max(t1,t2);
				}
				else ans=a*aa[0].num+b*bb[0].num;
			}
			else if(b>0){
				if(aa[0].index==bb[n-1].index) {
					 t1=a*aa[1].num+b*bb[n-1].num;
					 t2=a*aa[0].num+b*bb[n-2].num;
					 ans=max(t1,t2);
				}
				else ans=a*aa[0].num+b*bb[n-1].num;
			}
		}
		else if(a>0){
			if(b<0) {
				if(aa[n-1].index==bb[0].index) {
					 t1=a*aa[n-2].num+b*bb[0].num;
					 t2=a*aa[n-1].num+b*bb[1].num;
					ans=max(t1,t2);
				}
				else ans=a*aa[n-1].num+b*bb[0].num;
			}
			else {
				if(aa[n-1].index==bb[n-1].index) {
					t1=a*aa[n-2].num+b*bb[n-1].num;
					 t2=a*aa[n-1].num+b*bb[n-2].num;
					ans=max(t1,t2);
				}
				else ans=aa[n-1].num*a+b*bb[n-1].num;
			}
		}
		printf("Case #%d: %I64d\n",z,ans);
	
	}
	return 0;
}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值