GCJ:2008 Round1AA-Minimum Scalar Product(有序数组倒序乘积和最小)
题目链接: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
解题心得:
- 第一次做GCJ的题目,这个题目的意思就是给你两个数组,你可以改变数组内任意一个元素的位置,要求a[1]*b[1]+a[2]*b[2]+a[3]*b[3]….a[n]*b[n]的值最小。
- 刚开始还去讨论判断了一下,其实根本不用讨论,就是有序数组倒序乘积和最小,也就是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(有序数组倒序乘积和最小)的更多相关文章
- [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 ...
- codejam环境熟悉—Minimum Scalar Product
今天准备熟悉一下codejam的在线编程,为google的笔试做准备,因此按照codejam上对新手的建议,先用了一个简单的题目来弄清楚流程.记录一下需要注意的地方. 1.输入输出 输入输出重定位 ...
- GCJ 2008 Round 1A Minimum Scalar Product
https://code.google.com/codejam/contest/32016/dashboard 题目大意: GCJ(google code jam)上的水题.下周二有比赛,来熟悉熟悉. ...
- GCJ——Minimum Scalar Product(2008 Round1 AA)
题意: 给定两组各n个数,可任意调整同一组数之中数字的顺序,求 sum xi*yi i=1..n的最小值. Small: n<=8 abs xy,yi<=1000 Large: n< ...
- GCJ 2008 Round 1A Minimum Scalar Product( 水 )
链接:传送门 题意:给两个向量 v1 = { x1 , x2 , x3 , x4 .... } , v2 = { y1 , y2 , y3 , y4 ...... } 允许任意交换 v1 和 v2 各 ...
- [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 ...
- 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. ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...
- [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. ...
随机推荐
- EF--Model First
Model First先设计Model对象,再由对象生成数据库. 1.新建控制台项目,名称ModelFirst,确定. 2.点击选中项目,右键-->添加-->新建项目--选择数据模板--& ...
- zabbix 编译安装指导
zabbix 编译安装 下载 安装 安装后的配置 下载源码包 zabbix官网:https://www.zabbix.com/ zabbix下载:https://www.zabbix.com/down ...
- 分布式系统ID生成方案汇总
在分布式系统中,需要对大量的数据.消息.请求等进行唯一的标识,例如分布式数据库的ID需要满足唯一且多数据库同步,在单一系统中,使用数据库自增主键可以满足需求,但是在分布式系统中就需要一个能够生成全局唯 ...
- Java集合集锦
1.介绍Collection框架的结构 集合是Java中的一个非常重要的一个知识点,主要分为List.Set.Map.Queue三大数据结构.它们在Java中的结构关系如下: Collection接口 ...
- 翻译-ExcelDNA开发文档-首页
转载自个人主页 前言 ExcelDNA是一名国际友人开发的开源框架,文档全是英文文档,当时看的时候非常吃力,现在将英文文档翻译过来,为的是让自己加深印象以及自己以后看的时候能不用这么吃力. 介绍 Ex ...
- 简单的NLog配置文件
NLog.config <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="h ...
- IOS Get请求(请求服务器)
@interface HMViewController () <NSURLConnectionDataDelegate> @property (weak, nonatomic) IBOut ...
- hdu-3015 Disharmony Trees---离散化+两个树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的ra ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) H】命命命运(概率DP)
点此看题面 大致题意: 有\(6\)个人玩大富翁,共有\(n\)块地,进行\(500\)轮,已知每个人掷骰子掷出\(1\sim6\)的概率.当某人到达一块未被占领的地时,他可以占领它.求最后每个人占有 ...
- BZOJ 3233: [Ahoi2013]找硬币
BZOJ 3233: [Ahoi2013]找硬币 标签(空格分隔): OI-BZOJ OI-DP Time Limit: 10 Sec Memory Limit: 64 MB Description ...