题目链接:https://code.google.com/codejam/contest/32016/dashboard#s=p0


Minimum Scalar Product

This contest is open for practice. You can try every problem as many times as you like, though we won’t keep track of which problems you solve. Read the Quick-Start Guide to get started.

Problem

You are given two vectors v1=(x1,x2,…,xn) and v2=(y1,y2,…,yn). The scalar product of these vectors is a single number, calculated as x1y1+x2y2+…+xnyn.

Suppose you are allowed to permute the coordinates of each vector as you wish. Choose two permutations such that the scalar product of your two new vectors is the smallest possible, and output that minimum scalar product.

Input

The first line of the input file contains integer number T - the number of test cases. For each test case, the first line contains integer number n. The next two lines contain n integers each, giving the coordinates of v1 and v2 respectively.

Output

For each test case, output a line

Case #X: Y

where X is the test case number, starting from 1, and Y is the minimum scalar product of all permutations of the two given vectors.

Limits

Small dataset

T = 1000

1 ≤ n ≤ 8

-1000 ≤ xi, yi ≤ 1000

Large dataset

T = 10

100 ≤ n ≤ 800

-100000 ≤ xi, yi ≤ 100000

Sample

Input

2

3

1 3 -5

-2 4 1

5

1 2 3 4 5

1 0 1 0 1

Output

Case #1: -25

Case #2: 6


解题心得:

  1. 第一次做GCJ的题目,这个题目的意思就是给你两个数组,你可以改变数组内任意一个元素的位置,要求a[1]*b[1]+a[2]*b[2]+a[3]*b[3]….a[n]*b[n]的值最小。
  2. 刚开始还去讨论判断了一下,其实根本不用讨论,就是有序数组倒序乘积和最小,也就是sum(a[i]*b[n-i+1])的值。下面进行了部分证明,其实拓展之后也是适用的,懒得写了。


代码:

#include <algorithm>
#include <stdio.h>
#include <sudo_plugin.h> using namespace std;
typedef long long ll; const int maxn = 1000; ll num1[maxn],num2[maxn]; int main() {
FILE *fp,*fout;
fp = fopen("A-large-practice.in","r");
fout = fopen("A.out","w");
int t,T = 1;
fscanf(fp,"%d",&t);
while(t--) {
int n;
fscanf(fp,"%d",&n);
for (int i = 0; i < n; i++)
fscanf(fp,"%lld",&num1[i]);
for (int i = 0; i < n; i++)
fscanf(fp,"%lld",&num2[i]);
sort(num1, num1 + n);
sort(num2, num2 + n);
int r = n - 1;
ll sum = 0;
for (int i = 0; i < n; i++) {
sum += num1[i] * num2[r--];
}
fprintf(fout,"Case #%d: %lld\n",T++ , sum);
}
fclose(fout);
fclose(fp);
return 0;
}

最新文章

  1. Hibernate4.0之HibernateSessionFactory源码详解
  2. JavaWeb结合七牛云存储搭建个人相册服务
  3. 故障处理-ORA-00376/ORA-01110
  4. HDOJ 1083 Courses
  5. console的一个小易错点
  6. C#绘图双缓冲
  7. How to write a product backlog step by step
  8. 十六进制转十进制 - C
  9. win7 cmd命令行窗口 宽度无法变大 自由调整大小
  10. strncpy 用法
  11. C++ Reference 的“三位一体”诠释
  12. SSM整合中遇到的不能扫描注解的bug
  13. TCP报文中的SYN,FIN,ACK,PSH,RST,URG
  14. hive 动态分区数设置
  15. pl/sql报错快速解决方法(新手推荐)
  16. Python脱产8期 Day014 2019/4/28
  17. python访问百度地图接口并返回信息
  18. ios开发之--NSString中substringFromIndex,substringWithRange,substringToIndex方法的使用
  19. Vue复选框的全选
  20. Spark SQL利器:cacheTable/uncacheTable【转】

热门文章

  1. Linux 学习 之 bash
  2. JSON中不能加注释
  3. SonarQube代码质量管理平台介绍与搭建
  4. SharePoint 2010 网络上的开发经验和资源
  5. springboot实现邮件发送
  6. QT学习之QScript
  7. Apache2.4 authz_core_module模块使用
  8. Computer Science: the Big Picture
  9. IOS 音频的 使用说明
  10. 位图算法-hash算法的后继应用