zoukankan      html  css  js  c++  java
  • 1113 Integer Set Partition (25 分)集合分割

    1113 Integer Set Partition (25 分)

    Given a set of NNN (>1> 1>1) positive integers, you are supposed to partition them into two disjoint sets A1A_1A1​​ and A2A_2A2​​ of n1n_1n1​​ and n2n_2n2​​ numbers, respectively. Let S1S_1S1​​ and S2S_2S2​​ denote the sums of all the numbers in A1A_1A1​​ and A2A_2A2​​, respectively. You are supposed to make the partition so that ∣n1−n2∣|n_1 - n_2|n1​​n2​​∣ is minimized first, and then ∣S1−S2∣|S_1 - S_2|S1​​S2​​∣ is maximized.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives an integer NNN (2≤N≤1052 le N le 10^52N105​​), and then NNN positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 2312^{31}231​​.

    Output Specification:

    For each case, print in a line two numbers: ∣n1−n2∣|n_1 - n_2|n1​​n2​​∣ and ∣S1−S2∣|S_1 - S_2|S1​​S2​​∣, separated by exactly one space.

    Sample Input 1:

    10
    23 8 10 99 46 2333 46 1 666 555
    

    Sample Output 1:

    0 3611
    

    Sample Input 2:

    13
    110 79 218 69 3721 100 29 135 2 6 13 5188 85
    

    Sample Output 2:

    1 9359

    思路:
      由题目可知,当n是偶数时,n1=n2;当n是奇数时,n2-n1=1,计算abs(sum1-sum2),首先对整个集合进行排序,
    然后计算即可。
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<map>
    #include<set>
    using namespace std;
    
    
    
    int main()
    {
        int n;
        cin>>n;
        int a[n+1];
        for(int i=1;i<n+1;i++)
            cin>>a[i];
        sort(a+1,a+n+1);
    
        if(n%2==0)
        {
            long long int sum=0;
            for(int i=1;i<=n/2;i++)
            {
                sum+=a[n-i+1]-a[i];
            }
            cout<<"0 "<<sum;
        }
        else
        {
            long long int sum=0;
            for(int i=1;i<=n/2;i++)
            {
                sum+=a[n-i+1]-a[i];
            }
            sum+=a[n/2+1];
            cout<<"1 "<<sum;
        }
        return 0;
    }


  • 相关阅读:
    每天进步一点点------Xilinx IP 内核
    每天进步一点点------Xilinx DCM
    每天进步一点点------Modelsim添加Xilinx仿真库的详细步骤
    每天进步一点点------ISE 12.4的FPGA设计基本流程
    每天进步一点点------ModelSim仿真Altera的ROM
    每天进步一点点------FIR(一)
    每天进步一点点------离散余弦变换(DCT) (一)
    Busybox构建根文件系统和制作Ramdisk
    使用NFS启动Tiny4412开发板根文件系统
    Ubuntu14.04环境下配置TFTP服务器
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10303814.html
Copyright © 2011-2022 走看看