PAT 1113 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 n?2 numbers, respectively. Let S1? and S?2 denote the sums of all the numbers in A?1?? and A?2?? , respectively. You are supposed to make the partition so that ∣n?1?? ?n?2?? ∣ is minimized first, and then ∣S?1?? ?S?2?? ∣ is maximized.

Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤10^5?? ), 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 2^?31.

Output Specification:
For each case, print in a line two numbers: ∣n?1? 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

#include<iostream> //水题
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(const int &a, const int &b){
    return a>b;
}
int main(){
    int N, sum=0;
    cin>>N;
    vector<int> vec(N, 0);
    for(int i=0; i<N; i++)
        cin>>vec[i];
    sort(vec.begin(), vec.end(), cmp);
    int t=N%2==0?N/2:N/2+1;
    for(int i=0; i<N; i++)
        if(i<t)
            sum+=vec[i];
        else
            sum-=vec[i];
    cout<<2*t-N<<" "<<sum<<endl;
    return 0;
} 

原文地址:https://www.cnblogs.com/A-Little-Nut/p/9502011.html

时间: 2024-08-30 16:01:54

PAT 1113 Integer Set Partition的相关文章

1113 Integer Set Partition (25 分)

1113 Integer Set Partition (25 分) Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets A?1?? and A?2?? of n?1?? and n?2?? numbers, respectively. Let S?1?? and S?2?? denote the sums of all the numbers in A?

PAT (Advanced Level) 1113. Integer Set Partition (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long a[100000+10]; long long sum=0,sum2; i

PAT A1113 Integer Set Partition (25 分)——排序题

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A?1?? and A?2?? of n?1?? and n?2?? numbers, respectively. Let S?1?? and S?2?? denote the sums of all the numbers in A?1?? and A?2??, respectively. You

PAT A1113 Integer Set Partition [模拟]

题目描述 链接 给一堆数,分成两堆,要求两堆数目差值最小,其次追求两堆的差值尽量大 分析 排序 代码 #include<bits/stdc++.h> using namespace std; int n; const int maxn = 1e5+10; int a[maxn]; int main(){ cin>>n; int s1 = 0, s2 = 0; for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n); for(i

PAT 1103 Integer Factorization[难]

1103 Integer Factorization(30 分) The K?P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K?P factorization of N for any positive integers N, K an

PAT 1103 Integer Factorization

The K?P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K?P factorization of N for any positive integers N, K and P. Input Specification: Each in

A1113 | Integer Set Partition (25)

太简单了 #include <stdio.h> #include <memory.h> #include <math.h> #include <string> #include <vector> #include <set> #include <stack> #include <queue> #include <algorithm> #include <map> #define I sc

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

Redis 键(key)相关的命令及其它命令的查看地址

Redis 键(key) Redis 键命令用于管理 redis 的键. 语法 Redis 键命令的基本语法如下: redis 127.0.0.1:6379> COMMAND KEY_NAME 实例: 在以上实例中 DEL 是一个命令, runoobkey 是一个键. 如果键被删除成功,命令执行后输出 (integer) 1,否则将输出 (integer) 0 下面给大家列举几个常用的跟键相关的命令: Redis DEL 命令 Redis DEL 命令用于删除已存在的键.不存在的 key 会被忽