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

时间:2023-02-17 00:10:02

Problem A. 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 
 
Output 
 
2
3
1 3 -5
-2 4 1
5
1 2 3 4 5
1 0 1 0 1

Case #1: -25
Case #2: 6
题解:排序不等式的应用。
 
排序不等式公式
0<a1<a2<a3......<an
0<b1<b2<b3......<bn
an×bn+an-1×bn-1+......+a1×b1>=乱序和>=a1×bn+a2×bn-1+......+an×b1
(注:n,n-1,n-2等,均为角标)
 
顺序不等式基本形式:
[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product

排序不等式的证明

分析法
要证
[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
只需证
[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
根据基本不等式
[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
只需证
[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
∴原结论正确
 
代码:
 #include<stdio.h>
#include<string.h> int i,j,n,m; long long a[],b[]; long long sum; int
pre()
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
sum=;
return ;
} int
init()
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%lld",&a[i]);
for(i=;i<=n;i++)
scanf("%lld",&b[i]); return ;
} void
qsort(long long a[],int head,int tail)
{
int i,j;
long long x;
i=head;j=tail;
x=a[head]; while(i<j)
{
while((i<j)&(a[j]>=x)) j--;
a[i]=a[j];
while((i<j)&(a[i]<=x)) i++;
a[j]=a[i];
}
a[i]=x; if(head<(i-)) qsort(a,head,i-);
if((i+)<tail) qsort(a,i+,tail);
} int
main()
{
int casi,cas;
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
scanf("%d",&cas);
for(casi=;casi<=cas;casi++)
{
pre();
init();
qsort(a,,n);
qsort(b,,n); for(i=;i<=n;i++)
sum+=a[i]*b[n-i+]; printf("Case #%d: %lld\n",casi,sum);
}
return ;
}
 
 
 

[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product的更多相关文章

  1. Google Code Jam Round 1A 2015 解题报告

    题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...

  2. Google Code Jam Round 1A 2015 Problem B&period; Haircut 二分

    Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...

  3. 【二分答案】Google Code Jam Round 1A 2018

    题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...

  4. 【贪心】Google Code Jam Round 1A 2018 Waffle Choppers

    题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等. 设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1). 先 ...

  5. Google Code Jam Round 1C 2015 Problem A&period; Brattleship

    Problem You're about to play a simplified "battleship" game with your little brother. The ...

  6. &lbrack;C&plus;&plus;&rsqb;Saving the Universe——Google Code Jam Qualification Round 2008

    Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...

  7. &lbrack;Google Code Jam &lpar;Qualification Round 2014&rpar; &rsqb; B&period; Cookie Clicker Alpha

    Problem B. Cookie Clicker Alpha   Introduction Cookie Clicker is a Javascript game by Orteil, where ...

  8. &lbrack;C&plus;&plus;&rsqb;Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  9. Google Code Jam Africa 2010 Qualification Round Problem B&period; Reverse Words

    Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...

随机推荐

  1. 【转】【C&num;】C&num;性能优化总结

    1.  C#语言方面         1.1 垃圾回收    垃圾回收解放了手工管理对象的工作,提高了程序的健壮性,但副作用就是程序代码可能对于对象创建变得随意.    1.1.1 避免不必要的对象创 ...

  2. C&num;多线程之二:ManualResetEvent和AutoResetEvent

    初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...

  3. Openstack Swift中间件编写

    关于openstack swift的资料可以看这里,这里还有这里. 准备环境 从零开始接触的同学可以先从swift的all in one部署开始学习,在本机搭建好swift环境就可以进行简单的测试了. ...

  4. 动态链接库&lpar;dll&rpar; &lowbar;&lowbar;declspec&lpar;dllimport&rpar; &lowbar;&lowbar;declspec&lpar;dllexport&rpar;

    一. __declspec(dllexport) Microsoft 在 Visual C++ 的 16 位编译器版本中引入了 __export,使编译器得以自动生成导出名并将它们放到一个 .lib ...

  5. puppet安装和使用

    puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描写叙述语言,可管理配置 文件.用户.cron任务.软件包.系统服务等.puppet把这些系统实体称 ...

  6. mybatis sql in not in的使用

    xml配置 <select id="SelectAllByNotsampleNo" resultMap="BaseResultMap" parameter ...

  7. python 版Faster Rcnn

    直接按照官网https://github.com/rbgirshick/py-faster-rcnn上的教程对faster Rcnn进行编译的时候,会发有一些层由于cudnn版本的更新,会报错如下: ...

  8. 【liunx】sftp常用命令

    sftp是Secure FileTransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分, ...

  9. Centos 安装Sublime text 3

    1.下载sublime3 http://www.sublimetext.com/3 2.解压缩,即为安装. # cd /opt # tar xjf /home/alex/Downloads/subli ...

  10. 【洛谷】NOIP提高组模拟赛Day1【组合数学】【贪心&plus;背包】【网络流判断是否满流以及流量方案】

    U41568 Agent1 题目背景 2018年11月17日,中国香港将会迎来一场XM大战,是世界各地的ENLIGHTENED与RESISTANCE开战的地点,某地 的ENLIGHTENED总部也想派 ...