题目大意:
现有T个测例,每个测例都给定两个大小为N的一维向量x和y,现可以重新排列向量中元素的顺序,要求出两向量可能的最小的点积值。
small:T = 1000, 1 ≤ N ≤ 8, -1000 ≤ xi, yi ≤ 1000
large:T = 10, 100 ≤ N ≤ 800, -100000 ≤ xi, yi ≤ 100000
注释代码:
/*
* Problem ID : GCJ Round 1A 2008 Problem A. Minimum Scalar Product
* Author : Lirx.t.Una
* Language : G++
*/
#include <algorithm>
#include <iostream>
#include <cstdio>
#define MAXSIZE 800
using namespace std;
typedef __int64 llg;
int v1[MAXSIZE];
int v2[MAXSIZE];
int
main() {
int nscn, iscn;
int n;//向量的大小
int i;//计数变量
llg ans;//保存向量点乘的最终结果
//方法就是先排序,然后让最大的和最小的乘,求和即可
#ifdef __SMALL__
freopen("A-small-practice.in", "r", stdin);//将标准输入输出流重定向到文件中
freopen("A-small-practice.out", "w", stdout);//是的scanf、printf都以文件作为对象
#endif
#ifdef __LARGE__
freopen("A-large-practice.in", "r", stdin);
freopen("A-large-practice.out", "w", stdout);
#endif
scanf("%d", &nscn);
iscn = 0;
while ( nscn-- ) {
scanf("%d", &n);
for ( i = 0; i < n; i++ ) scanf("%d", v1 + i);
for ( i = 0; i < n; i++ ) scanf("%d", v2 + i);
sort(v1, v1 + n);
sort(v2, v2 + n);
ans = 0;
for ( i = 0; i < n; i++ )
ans += (llg)v1[i] * (llg)v2[n - i - 1];
printf("Case #%d: %I64d\n", ++iscn, ans);
}
fclose(stdin);
fclose(stdout);
return 0;
}
无注释代码:
#include <algorithm>
#include <iostream>
#include <cstdio>
#define MAXSIZE 800
using namespace std;
typedef __int64 llg;
int v1[MAXSIZE];
int v2[MAXSIZE];
int
main() {
int nscn, iscn;
int n;
int i;
llg ans;
#ifdef __SMALL__
freopen("A-small-practice.in", "r", stdin);
freopen("A-small-practice.out", "w", stdout);
#endif
#ifdef __LARGE__
freopen("A-large-practice.in", "r", stdin);
freopen("A-large-practice.out", "w", stdout);
#endif
scanf("%d", &nscn);
iscn = 0;
while ( nscn-- ) {
scanf("%d", &n);
for ( i = 0; i < n; i++ ) scanf("%d", v1 + i);
for ( i = 0; i < n; i++ ) scanf("%d", v2 + i);
sort(v1, v1 + n);
sort(v2, v2 + n);
ans = 0;
for ( i = 0; i < n; i++ )
ans += (llg)v1[i] * (llg)v2[n - i - 1];
printf("Case #%d: %I64d\n", ++iscn, ans);
}
fclose(stdin);
fclose(stdout);
return 0;
}
单词解释:
permutation:n, 全排列,置换
permute:vt, 交换,排列
scalar:adj, 标量的,数量的
product:n, 乘积
scalar product:n, 数量积,点积