zoukankan      html  css  js  c++  java
  • 算法训练 最小乘积(基本型)

    http://lx.lanqiao.org/problem.page?gpid=T133

    算法训练 最小乘积(基本型)  
    时间限制:1.0s   内存限制:512.0MB
        
    问题描述
      给两组数,各n个。
      请调整每组数的排列顺序,使得两组数据相同下标元素对应相乘,然后相加的和最小。要求程序输出这个最小值。
      例如两组数分别为:1 3  -5和-2 4 1

      那么对应乘积取和的最小值应为:
      (-5) * 4 + 3 * (-2) + 1 * 1 = -25
    输入格式
      第一个行一个数T表示数据组数。后面每组数据,先读入一个n,接下来两行每行n个数,每个数的绝对值小于等于1000。
      n<=8,T<=1000
    输出格式
      一个数表示答案。
    样例输入
    2
    3
    1 3 -5
    -2 4 1
    5
    1 2 3 4 5
    1 0 1 0 1
    样例输出
    -25
    6
     
    分析:
    排序,然后最小值和最大值相成;
     
    AC代码:
     
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <string>
     6 #include <math.h>
     7 #include <stdlib.h>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 #include <map>
    12 #include <list>
    13 #include <iomanip>
    14 #include <vector>
    15 #pragma comment(linker, "/STACK:1024000000,1024000000")
    16 #pragma warning(disable:4786)
    17 
    18 using namespace std;
    19 
    20 const int INF = 11;
    21 const int Max = 10000 + 10;
    22 const double eps = 1e-8;
    23 const double PI = acos(-1.0);
    24 
    25 int a[INF] , b[INF];
    26 
    27 int main()
    28 {
    29     int T , n;
    30     scanf("%d",&T);
    31     while(T --)
    32     {
    33         scanf("%d", &n);
    34         int i , j;
    35         for(i = 0;i < n;i ++)
    36             scanf("%d",&a[i]);
    37         for(i = 0;i < n;i ++)
    38             scanf("%d",&b[i]);
    39         sort(a , a + n);
    40         sort(b , b + n);
    41         int ans = 0;
    42         for(i = 0 , j = n - 1 ;i < n;i ++)
    43                 ans += a[i] * b[j --];
    44         printf("%d
    ",ans);
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    java冒泡排序-选择排序-插入排序-使用API中文文档直接调用函数
    java数组回顾---线性查找最大值最小值---二分查找
    java万年历
    java基础易混点
    progressbar
    TabHost
    ExpandableListView
    AutoCompleteTextView
    GridView
    图像视图-ImageView
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4450504.html
Copyright © 2011-2022 走看看