934A - A Compatible Pair - CodeForces Solution


brute force games *1400

Please click on ads to support us..

Python Code:

n, m = map (int, input().split())
rub = list (map (int, input().split()))
sap = list (map (int, input().split()))

fst = False     snd = False     
for r in rub:
    rbest = r * sap[0]          for s in sap:
        rbest = max (rbest, r * s)
        if fst == False:
        fst = rbest
    elif rbest > fst:
        snd = fst
        fst = rbest
    elif snd == False or rbest > snd:
        snd = rbest
        
print (snd)

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define TESTCASES false
void solve(int tt) {
    int n, m;
    cin >> n >> m;

    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<long long> b(m);
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }

    vector<pair<long long, int>> comb(n * m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            comb[(i * m) + j] = {a[i] * b[j], i};
        }
    }

    sort(comb.rbegin(), comb.rend());

    int i = 1;
    while (i < comb.size() && comb[i].second == comb[0].second)
        i++;

    if (i == comb.size())
        i--;
    cout << comb[i].first << "\n";
}

int main() {
    if (!TESTCASES) {
        solve(0);
        return 0;
    }

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve(i + 1);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction