Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.
Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a1, a2, ..., an, and Banban's have brightness b1, b2, ..., bm respectively.
Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.
Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.
You are asked to find the brightness of the chosen pair if both of them choose optimally.
The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50).
The second line contains n space-separated integers a1, a2, ..., an.
The third line contains m space-separated integers b1, b2, ..., bm.
All the integers range from - 109 to 109.
Print a single integer — the brightness of the chosen pair.
2 2 20 18 2 14
252
5 3 -1 0 1 2 3 -1 0 1
2
In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.
In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.
题意:Banban将会从Tommy的序列中取走一个数,和自己序列的一个数组成乘积最大。但是Tommy希望Banban最终组成的数尽可能小,而且它将会提前从自己序列中取走一个数以此来达到目的。
思路:题目有点绕,其实Tommy一开始希望结果尽可能小这句话我读到了,但是没能理解,解题时也没想到。一开始没想用暴力做,但是后来发现单独考虑的情况太多了,总是WA。暴力要两次,第一次是为了让Tommy取数,第二次是为了求结果。
一开始多此一举把loc列进vis[]里判断,但是后来runtime error,不知道什么原因
Exit code is -1073741819
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 1e18
typedef long long ll;
using namespace std;
ll n, m, a[110], b[110];
int main()
{
while(cin >> n >> m){
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
ll maxm = -INF, loc;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i]*b[j] > maxm){
maxm = a[i]*b[j];
loc=i;
}
}
}
maxm=-INF;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i==loc)
continue;
if(a[i]*b[j] >maxm){
maxm = a[i]*b[j];
}
}
}
cout << maxm << endl;
}
return 0;
}