Skip to content

Instantly share code, notes, and snippets.

@amoshyc
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save amoshyc/eaa247619a856ef4c2f5 to your computer and use it in GitHub Desktop.

Select an option

Save amoshyc/eaa247619a856ef4c2f5 to your computer and use it in GitHub Desktop.
Minimum Scalar Product(GCJ 2008 Round 1A)

#Minimum Scalar Product(GCJ 2008 Round 1A)

題目

To generate minimum scalar product , sort v1 in descending order while v2 in ascending order.

Code

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(false);

    int T;
    cin >> T;

    for (int case_=1; case_<=T; case_++) {
        int N;
        cin >> N;

        vector<long long> v1(N);
        vector<long long> v2(N);
        for (int i=0; i<N; i++)
            cin >> v1[i];
        for (int i=0; i<N; i++)
            cin >> v2[i];

        sort(v1.begin(), v1.end());
        sort(v2.begin(), v2.end());

        long long sum = 0;
        for (int i=0; i<N; i++)
            sum += v1[i] * v2[N-i-1];

        cout << "Case #" << case_ << ": " << sum << "\n";
    }

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment