码迷,mamicode.com
首页 > 其他好文 > 详细

PAT A1113 Integer Set Partition [模拟]

时间:2019-09-04 20:44:07      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:lse   main   int   namespace   cin   ons   include   problem   题目   

题目描述

链接
给一堆数,分成两堆,要求两堆数目差值最小,其次追求两堆的差值尽量大

分析

  • 排序

代码

#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));
}

PAT A1113 Integer Set Partition [模拟]

标签:lse   main   int   namespace   cin   ons   include   problem   题目   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!