zoukankan      html  css  js  c++  java
  • PAT甲级——A1113 Integer Set Partition

    Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets A1​​ and A2​​ of n1​​and n2​​ numbers, 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 ∣ is minimized first, and then ∣ is maximized.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives an integer N (2), and then Npositive 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: ∣ and ∣, 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 <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 int n, s1 = 0, s2 = 0, nMin, sMin;
     6 int main()
     7 {
     8     cin >> n;
     9     vector<int>v(n);
    10     for (int i = 0; i < n; ++i)
    11         cin >> v[i];
    12     sort(v.begin(), v.end());
    13     for (int i = 0; i < n; ++i)
    14     {
    15         if (i < n / 2)
    16             s1 += v[i];
    17         else
    18             s2 += v[i];
    19     }
    20     cout << n % 2 << " " << s2 - s1 << endl;
    21     return 0;
    22 }
  • 相关阅读:
    升级到 classpath 'com.android.tools.build:gradle:1.0.0-rc1
    OnScrollListenerPro
    dp和px的转换
    ListView 中判断是否滚动到底部
    为什么setAdapter之后不能addHeadView或者addfooterView
    SwipeRefreshLayout使用小记
    Git-Flow 带你飞!
    在aws的ec2服务器上搭建nginx+php的环境
    PHP程序员的技术成长规划
    GeoHash核心原理解析
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11455588.html
Copyright © 2011-2022 走看看