[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
Problem A. Minimum Scalar Product
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 |
Case #1: -25 |
排序不等式的证明
#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的更多相关文章
- Google Code Jam Round 1A 2015 解题报告
题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...
- Google Code Jam Round 1A 2015 Problem B. Haircut 二分
Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...
- 【二分答案】Google Code Jam Round 1A 2018
题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...
- 【贪心】Google Code Jam Round 1A 2018 Waffle Choppers
题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等. 设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1). 先 ...
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
- [C++]Saving the Universe——Google Code Jam Qualification Round 2008
Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...
- [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha
Problem B. Cookie Clicker Alpha Introduction Cookie Clicker is a Javascript game by Orteil, where ...
- [C++]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 ...
- Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words
Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...
随机推荐
- objc runtime 动态增加属性
objective-c中,有类别可以在不修改源码的基础上增加方法:近排在看别人的开源代码时,发现还可以动态增加属性.而且是在运行时,太牛B了. 使用运行时库,必须要先引入 objc/runtime.h ...
- pfsense下的流量管理(转)
http://www.pppei.net/blog/post/331 在作流量管理时,这些概念很重要,不要迷失.. 这里再对Limiter 的源地址和目的地址做个说明,因为limiter是被应用在La ...
- PYTHON线程知识再研习G--线程间通信Event
很多时候,线程之间会有互相通信的需要.常见的情形是次要线程为主要线程执行特定的任务,在执行过程中需要不断报告执行的进度情况.前面的条件变量同步已经涉及到了线程间的通信(threading.Condit ...
- poj1704
题目大意:n个石子,每次可以取一个石子向左移动,左边有边界限制,每个位置最多同时放一个石子,求先手必胜还是必败. 首先,我们将石子两两配对,每一对,若是先手可以将左边的向左移动一格,则可以用后手将右边 ...
- ECMAScript 5/6/7兼容性速查表
http://kangax.github.io/compat-table/es5/ 秒查ECMAScript在各大浏览器的兼容性,点击右上角按钮可以“在5/6/7/非标”之间切换.做JavaScrip ...
- Android之ExpandableListView的属性(Group不展开)
1. 设置ExpandableListView 默认是展开的: 先实例化exListView 然后 exListView.setAdapter(exlvAdapter); //遍历所有group,将 ...
- bzoj 1195
http://www.lydsy.com/JudgeOnline/problem.php?id=1195 状压DP. 首先去掉被包含的字符串. 对于字符串i和j,我们求出 当字符串j的左端点在字符串i ...
- 关于bootstrap--表格(table的各种样式)
1.table-striped:斑马线表格 2.table-bordered:带边框的表格 3.table-hover:鼠标悬停高亮的表格 4.table-condensed:紧凑型表格(单元格的内距 ...
- 手动同步chrome浏览器
chrome浏览器每次设置好的标签在重新开机后都会变回设置前的状态,崩溃,每次设置好后还是手动同步一下吧. 1. 点击 工具(右上角的三个点)-->设置 2. 点击 高级同步设置 3. 点击 使 ...
- jsf标签,jsp标签与jstl标签
JSF通过定制标签与JSP集成.之前展示过的所有 JSF标签,<h:inputText>.<h:outputText>.<h:form> 和<f:view&g ...