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(int i=0;i<n;i++){
        if(i<n/2) s1+=a[i];
        else s2+=a[i];
    }
    if(n%2==0){
        printf("0 ");
    }
    else{
        printf("1 ");
    }
    printf("%d\n",abs(s2-s1));
}

原文地址:https://www.cnblogs.com/doragd/p/11461239.html

时间: 2024-08-27 19:03:06

PAT A1113 Integer Set Partition [模拟]的相关文章

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 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

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

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团体程序设计天梯赛 - 模拟赛

由于本人愚笨,最后一题实在无力AC,于是只有前14题的题解Orz 总的来说,这次模拟赛的题目不算难,前14题基本上一眼就有思路,但是某些题写起来确实不太容易,编码复杂度有点高~ L1-1 N个数求和 设计一个分数类,重载加法运算符,注意要约分,用欧几里得算法求个最大公约数即可. 1 #include <cstdio> 2 3 long long abs(long long x) 4 { 5 return x < 0 ? -x : x; 6 } 7 8 long long gcd(long

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

PAT 1014 Waiting in Line (模拟)

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are: The space inside the yellow line in front of each window is