Largest Point

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3065    Accepted Submission(s): 1078

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 i≠j 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 (2≤n≤5×106), a (0≤|a|≤106) and b (0≤|b|≤106). The second line contains n integers t1,t2,⋯,tn where 0≤|ti|≤106 for 1≤i≤n.

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
 
Source
 
 
题意:
  求a*ti*ti+b*tj的最大值,ti,tj是num数组里的两个数
分析:
  分情况考虑a和b为正为负的情况,当a,b为正时ti,tj要尽量大,因为a*ti*ti的影响大于b*tj,所以ti优先取最大值,在ti取完最大值后tj再来取剩下的最大值(这里用个map判断是否还剩余最大值,最大值可能有多个或者ti取了负的最大值)
  然后是为负的情况用同样的方法计算就行
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
#define inf 0x17a6e6736e9
using namespace std;
const int maxn = 5*1e6 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
ll s1[maxn], s2[maxn];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll T, t = 0;
cin >> T;
while( T -- ) {
t ++;
ll n, a, b;
cin >> n >> a >> b;
map<ll,ll> mp;
for( ll i = 0; i < n; i ++ ) {
cin >> s1[i];
s2[i] = abs(s1[i]);
mp[s1[i]] ++;
}
sort( s2, s2+n );
sort( s1, s1+n );
ll sum = 0;
if( a > 0 ) {
sum += a*s2[n-1]*s2[n-1];
if( b > 0 ) {
if( mp[-s2[n-1]] ) {
mp[-s2[n-1]] --;
} else {
mp[s2[n-1]] --;
}
if( mp[s1[n-1]] > 0 ) {
sum += b*s1[n-1];
} else {
sum += b*s1[n-2];
}
} else if( b < 0 ) {
if( mp[s2[n-1]] ) {
mp[s2[n-1]] --;
} else {
mp[-s2[n-1]] --;
}
if( mp[s1[0]] > 0 ) {
sum += b*s1[0];
} else {
sum += b*s1[1];
}
}
} else if( a < 0 ) {
sum += a*s2[0]*s2[0];
if( b > 0 ) {
if( mp[-s2[0]] ) {
mp[-s2[0]] --;
} else {
mp[s2[0]] --;
}
if( mp[s1[n-1]] > 0 ) {
sum += b*s1[n-1];
} else {
sum += b*s1[n-2];
}
} else if( b < 0 ) {
if( mp[s2[0]] ) {
mp[s2[0]] --;
} else {
mp[-s2[0]] --;
}
if( mp[s1[0]] > 0 ) {
sum += b*s1[0];
} else {
sum += b*s1[1];
}
}
}
cout << "Case #" << t << ": " << sum << endl;
}
return 0;
}

  

HDU5461 Largest Point 思维 2015沈阳icpc的更多相关文章

  1. HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc

    Best Solver Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Tota ...

  2. Largest Point (2015沈阳赛区网络赛水题)

    Problem Description Given the sequence A with n integers t1,t2,⋯,tn. Given the integral coefficients ...

  3. hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online

    Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...

  4. (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memo ...

  5. (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)

    http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others)  ...

  6. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 记2017沈阳ICPC

    2017沈阳ICPC 10月20日 早上十点抵达沈阳,趁着老师还没到,跑去故宫游玩了一下,玩到一点多回到宾馆,顺便吃了群里大佬说很好吃的喜家德虾饺(真的好好吃),回到宾馆后身体有点不舒服了,头晕晕的, ...

  8. Minimum Cut(2015沈阳online)【贪心】

    Minimum Cut[贪心]2015沈阳online 题意:割最少的边使得图不连通,并且割掉的边中有且仅有一条是生成树的边. 首先,我们选择一条树中的边进行切割,此时仅考虑树上的边集,有两种情况:1 ...

  9. hdu5461 Largest Point(沈阳网赛)

    Largest Point Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...

随机推荐

  1. Is it a full physical image???

    My friend asked me why she could not find some important files in a physical image acquired from an ...

  2. 微服务之springboot 自定义配置(一)Application配置文件

    配置的文件的格式 springboot可以识别两种格式的配置文件,分别是yml和properties 文件.我们可以将application.properties文件换成application.yml ...

  3. Java基础之二十 并发

    20.1 并发得多面性 并发编程令人困惑的一个主要原因:使用并发时需要解决的问题有多个,而实现并发的方法也有多种,并且在这两者之间没有明显的映射关系. 20.1.1 更快的执行 速度问题初听起来很简单 ...

  4. 章节十五、6-log4 2-用默认的配置

    一.实例演示 package log4jtutorial; import org.apache.logging.log4j.LogManager; import org.apache.logging. ...

  5. 【Java例题】5.5 两个字符串中最长公共子串

    5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...

  6. 解决oh-my-zsh中git分支显示乱码问题

    oh-my-zsh显示github分支时,如果当前文件夹不是git仓库,它就会显示乱码.倒腾了好几个小时终于弄清楚是oh-my-zsh中函数”git_prompt_info“的锅,然后又花了半个多小时 ...

  7. 不相交路径[BZOJ1471] 容斥原理 拓扑排序

    最近学容斥的时候又碰到一道类似的题目,所以想分享一个套路,拿这题来举例 [题目描述] 给出一个\(N(N\leq 150)\)个结点的有向无环简单图.给出4个不同的点\(a,b,c,d\),定义不相交 ...

  8. 一个web前端开发者的日常唠叨

    时间飞逝,距离上一次更新博客已经过去了三个月,上一篇博客的发布时间停留在了4月4日. 近来三个月没有更新博客,深感抱歉和愧疚.停更博客就意味着学习的越来越少,作为一个普通的前端开发者来说这是万万不可取 ...

  9. 洛谷 P2024 [NOI2001]食物链

    题意简述 有人用两种说法对这 N 个动物所构成的食物链关系进行描述: 1."1 X Y",表示 X 和 Y 是同类. 2."2 X Y",表示 X 吃 Y . ...

  10. 章节十六、1-TestNG简介

    一.TestNG 介绍 1.TestNG 是一个来自 JUnit 和 NUnit 的测试框架,它具拥有更多的功能,提高了 执行的效率. 2.TestNG 是一个开源的自动化测试框架 去除了老框架的大部 ...