题目链接: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. 【干货】Html与CSS入门学习笔记9-11

    九.盒模型 与元素亲密接触 1.盒模型 css将每个元素都看做一个盒子,包括以下属性: 内容区content area:包含内容,内容可以决定大小,也可以自行设定宽度和高度.元素的width属性指定的 ...

  2. 关于修改test9ui布局的一些小笔记

    今早,上IT修真园里,看到师兄大娃很负责任的将我任务里的项目的排版,3,6,7的列了出来. 谢谢师兄,那么负责任的照看师弟. 言归正传,我一开始,直接按照师兄的指示,选择性的优先修改底部.效果也达到了 ...

  3. Dynamics CRM 2011通过客户端代码选择视图

    在实施的过程中我们经常会遇到这样的场景,有个系统标准的Lookup字段对应的不是一种Entity,如很多地方的客户实际上可选account或者contact,有的地方可选systemuser或者tea ...

  4. 浅谈移动优先的跨终端Web 解决方案

    1.基准 我们定义测试基准和开发基准,也就是说我们定义我们在哪些浏览器上去进行调试. 左侧图主要是定义PC上的基准,其中A级项目中必须支持,B级可选,C级观察. 2.检测 主要是终端检测 这是一张架构 ...

  5. seleenium与Vs结合

    Vs 自带自动化测试录制工具.selenium则是开源的大众工具.在使用发现vs 自带的工具录制方便,但是修改很难.但是可以提供后续的BUG管理和邮件通知,自动构建等功能.selenium在.net平 ...

  6. strdup和strndup函数

    首先说明一下:这两个函数不建议使用,原因是返回内存地址把释放权交给别的变量,容易忘记释放. 一.strdup函数 函数原型 头文件:#include <string.h> char *st ...

  7. make知识

    makelist 语法 https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html CMakeLists.txt I am of t ...

  8. Oracle编程入门经典 第11章 过程、函数和程序包

    目录 11.1          优势和利益... 1 11.2          过程... 1 11.2.1       语法... 2 11.2.2       建立或者替换... 2 11.2 ...

  9. java调用dll库

    1.dll叫动态链接库,作用是用某种语言封装好某些函数生成可供不同语言调用的.dll文件,通常是用C++编写生成,因为C++可以对很多硬件操作方便而其他高级语言不行 2.dll生成参考:http:// ...

  10. sublime打开txt文件乱码的问题

    我们使用Sublime打开TXT文件的时候,会经常因为编码的问题造成乱码. 这是因为TXT记事本的默认保存编码格式是GBK,而Sublime text不支持GB2312和GBK编码. 我们可以通过安装 ...