Code Jam 2008 Round1A Problem A. Minimum Scalar Product —— 贪心
来源:互联网 发布:淘宝哪家二手3ds 编辑:程序博客网 时间:2024/04/30 09:37
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 nintegers each, giving the coordinates of v1 and v2 respectively.Output
For each test case, output a line
Case #X: Ywhere 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
简单的 贪心,给你两个数组,让你调整其中的位置使得a[0]*b[0] + a[1] * b[1] + ... 的值最小并输出最小值。
思路就是一个数组按照升序排,另一个按照降序排,然后他们的积之和最小。
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>using namespace std;#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid , rt << 1#define rson mid + 1 , r , rt << 1 | 1//#define mid ((l + r) >> 1)#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)#define bug(s) cout<<"#s = "<< s << endl;const int MAXN = 1010;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;#define eps 1e-8#define mod 1000000007typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pi;///#pragma comment(linker, "/STACK:102400000,102400000")int a[MAXN] , b[MAXN];int main(){ //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("A-large-practice .in","r",stdin); freopen("A-large-practice.out","w",stdout); #endif // Online_Judge int t , n; cin >> t; FORR(kase , 1 , t) { scanf("%d" , &n); FOR(i , 0 , n)scanf("%d" , &a[i]); FOR(i , 0 , n)scanf("%d" , &b[i]); sort(a , a + n); sort(b , b + n); LL ans = 0; FOR(i , 0 , n)ans += (LL)a[i] * b[n - i - 1]; printf("Case #%d: %I64d\n" , kase , ans); } return 0;}
- Code Jam 2008 Round1A Problem A. Minimum Scalar Product —— 贪心
- (code jam)Problem A. Minimum Scalar Product
- Code jam Problem A. Minimum Scalar Product
- Minimum Scalar Product(GCJ 2008 Round1A A)数学问题+贪心
- GCJ 2008 Round1A Problem A. Minimum Scalar Product
- Google code jam: Problem A. Minimum Scalar Product
- Minimum Scalar Product(2008 Round1A A)
- GCJ 2008 Round1A A Minimum Scalar Product
- Code Jam - Minimum Scalar Product for Python
- 125_minimum scalar product (2008 round1A A)
- GCJ 2008 Round 1A A - Minimum Scalar Product (贪心)
- GCJ Round 1A 2008 Problem A. Minimum Scalar Product
- GCJ Round 1A 2008 Problem A. Minimum Scalar Product
- Google Code Jam Notes - Minimum Scalar Product - Java
- 挑战2.7.1 Round 1A 2008 A. Minimum Scalar Product 贪心
- GCJ Round 1A 2008 Problem A. Minimum Scalar Product(YY)
- Code Jam 2009 Round2 Problem A. Crazy Rows —— 贪心
- 2008 Round1AA Minimum Scalar Product
- 子进程调用execv函数后,对信号的处理不保留
- C++字符串行编辑器分析
- iOS处理.9.png (点9图,NinePatch),类似Android
- unix信号处理函数 signal (1)
- linux grep命令
- Code Jam 2008 Round1A Problem A. Minimum Scalar Product —— 贪心
- oracle中的profile文件管理用户
- 基础篇:操作系统、计算机网络、设计模式
- Java 学习笔记(不断更新)
- HDU 2087 kmp 计数裸题
- 关于 android的 渲染器 Shader
- StarUML使用说明-指导手册
- Code Jam 2009 Round2 Problem A. Crazy Rows —— 贪心
- android 修改ramdisk.img和init.rc && android启动后设置/data权限为770