A - Easy Number Game ZOJ - 4004

题目:

The bored BaoBao is playing a number game. In the beginning, there are  numbers. For each turn, BaoBao will take out two numbers from the remaining numbers, and calculate the product of them.

Now, BaoBao is curious to know the minimum sum of the products if he plays at least m turns. Can you tell him?
Input
The first line of input contains a positive integer  (about 30), the number of test cases. For each test case:

The first line contains two integers  and  (, ). Their meanings are described above.

The second line contains  integers  (), indicating the numbers.

Output
For each test case output one integer, indicating the minimum sum of the products.

Sample Input
3
4 2
1 3 2 4
3 1
2 3 1
4 0
1 3 2 4
Sample Output
10
2
0
Hint
For the first sample test case, the answer is 1 × 4 + 3 × 2 = 10.

For the second sample test case, the answer is 2 × 1 = 2.

题解:
一开始的时候,我很天真的以为,只要头尾相乘就可以。然后保证不要溢出就行了,然而事实证明我太天真了。其实只要排序后保证

aia2mi+1 ∑ a i ∗ a 2 ∗ m − i + 1

这样就可以保证是最小的了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <queue>
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)

#define mset(var,val) memset(var,val,sizeof(var))

#define test(a) cout<<a<<endl
#define test2(a,b) cout<<a<<" "<<b<<endl

#define test3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
const int N= 2e5+7;
const int mod =1e9+7 ;
using namespace std;
int a[N];
void work(){
        int n,m;
        scdd(n,m);
        for(int i =1;i<=n;i++){
            scd(a[i]);
            // test(a[i]);
        }
        sort(a+1,a+n+1);
        long long ans=0;
        for(int i =1;i<=m;i++){
                ans = 1ll*(ans)+1ll*(1ll*a[i]*a[2*m-i+1]);
        }
        printf("%lld\n",ans);
}
int main(){
    #ifdef local
        freopen("in.txt","r",stdin);
    #endif
    int t;
    scd(t);
    while(t--){
        work();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值