题目链接: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;
}

GCJ:2008 Round1AA-Minimum Scalar Product(有序数组倒序乘积和最小)的更多相关文章

  1. [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product

    Problem A. Minimum Scalar Product   This contest is open for practice. You can try every problem as ...

  2. codejam环境熟悉—Minimum Scalar Product

    今天准备熟悉一下codejam的在线编程,为google的笔试做准备,因此按照codejam上对新手的建议,先用了一个简单的题目来弄清楚流程.记录一下需要注意的地方.   1.输入输出 输入输出重定位 ...

  3. GCJ 2008 Round 1A Minimum Scalar Product

    https://code.google.com/codejam/contest/32016/dashboard 题目大意: GCJ(google code jam)上的水题.下周二有比赛,来熟悉熟悉. ...

  4. GCJ——Minimum Scalar Product(2008 Round1 AA)

    题意: 给定两组各n个数,可任意调整同一组数之中数字的顺序,求 sum xi*yi i=1..n的最小值. Small: n<=8 abs xy,yi<=1000 Large: n< ...

  5. GCJ 2008 Round 1A Minimum Scalar Product( 水 )

    链接:传送门 题意:给两个向量 v1 = { x1 , x2 , x3 , x4 .... } , v2 = { y1 , y2 , y3 , y4 ...... } 允许任意交换 v1 和 v2 各 ...

  6. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  8. 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...

  9. [LeetCode] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. mybatis学习目录

    mybatis详解动态SQL https://www.cnblogs.com/ysocean/p/7289529.html mybatis查询时间段sql语句 http://www.cnblogs.c ...

  2. 翻译-ExcelDNA开发文档

    转载自个人主页 前言 翻译开源项目ExcelDNA开发文档 异步处理 ExcelDNA支持两种异步函数: RTD,该函数适用与Excel2003及以上版本,(当你使用ExcelAsyncUtil.*时 ...

  3. redis笔记(三)

     redis配置文件 配置文件对单位大小写不敏感 tcp-backlog  511   高并发环境连接数 tcp-keepalive   单位为秒 0 表示不会进行keepalive检测,,,建议设置 ...

  4. (原创)北美信用卡(Credit Card)个人使用心得与总结(个人理财版) [精华]

    http://forum.chasedream.com/thread-766972-1-1.html 本人2010年 8月F1 二度来美,现在credit score 在724-728之间浮动,最高的 ...

  5. Wampserver由橙变绿的解决过程

    因为C盘的内存问题,就重装了win7系统,那么就面临着很对软件要重新进行安装,安装wampserver时,再次遇到了服务器的图标一直是橙色的而不变绿色,安装包地址: http://download.c ...

  6. Altium_Designer17-PCB-如何重新定义板子外形

    当我们绘制完电路图.完成PCB布局和布线后,我们会遇到下面这种情况: 发现什么了吗?那么大的黑框框,我们只用到了很少的一部分.那有啥?告诉你黑框框的大小就是你最后将要印刷的PCB板的实际大小!大家都知 ...

  7. *187. Repeated DNA Sequences (hashmap, one for loop)(difference between subsequence & substring)

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  8. POJ-1509 Glass Beads---最小表示法模板

    题目链接: https://vjudge.net/problem/POJ-1509 题目大意: 给你一个循环串,然后找到一个位置,使得从这个位置开始的整个串字典序最小. 解题思路: 最小表示法模板 注 ...

  9. POJ-3669 Meteor Shower---BFS+预处理

    题目链接: https://vjudge.net/problem/POJ-3669 题目大意: 巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, ...

  10. 1.4 配置备份策略(Policy)

    1.1 配置备份策略(Policy) 一个备份策略由四部分组成. Attributes(属性) Policy是否Active Policy类型 由此Policy产生的任务的优先级 使用的Storage ...