zoukankan      html  css  js  c++  java
  • A1113. Integer Set Partition

    Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2numbers, respectively. Let S1 and S2 denote the sums of all the numbers in A1 and A2, respectively. You are supposed to make the partition so that |n1 - n2| is minimized first, and then |S1 - S2| is maximized.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives an integer N (2 <= N <= 105), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 231.

    Output Specification:

    For each case, print in a line two numbers: |n1 - n2| and |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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<string.h>
     5 using namespace std;
     6 int num[100001], N;
     7 int main(){
     8     scanf("%d", &N);
     9     for(int i = 0; i < N; i++){
    10         scanf("%d", &num[i]);
    11     }
    12     sort(num, num + N);
    13     int mid = N / 2;
    14     int sum1 = 0, sum2 = 0;
    15     for(int i = mid; i < N; i++){
    16         sum2 += num[i];
    17     }
    18     for(int i = mid - 1; i >= 0; i--){
    19         sum1 += num[i];
    20     }
    21     if(N % 2 == 0)
    22         printf("%d %d", 0, sum2 - sum1);
    23     else printf("%d %d", 1, sum2 - sum1);
    24     cin >> N;
    25     return 0;
    26 }
    View Code
  • 相关阅读:
    2.17-2.23第一周总结
    10号总结
    9日总结
    8号总结
    7号寒假总结
    6号
    读后感《程序员的修炼之道:从小工到专家》1
    java第二次动手动脑
    回文判断
    二进制的原码,反码以及补码介绍
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8574972.html
Copyright © 2011-2022 走看看