贪心|1113 Integer Set Partition (25分)

1113 Integer Set Partition (25分)

#include 
#include 
#include 
using namespace std;

vector<int>v;

int n;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int tmp;
		cin >> tmp;
		v.push_back(tmp);
	}
	sort(v.begin(), v.end());
	//1 8 10 23 46 46 99 555 666 2333
	//0 1 2  3  4  5  6  7   8   9
		//5
		//0 1 2 3 4
	//5
	//0 1 2 3 4 
		//2
		//0 1
	int sum1 = 0, sum2 = 0, sum = 0,sum_a=0,sum_b=0;
	for (int j = 0; j < n/2; j++) {
		sum1 += v[j];
	}
	for (int j = n/2; j < n; j++) {
		sum2 += v[j];
	}
		
	if (n % 2 == 0) {
		sum = abs(sum1 - sum2);
		cout << "0 " << sum;
	}else{
		sum_a = abs(sum1 - sum2);
		sum_b = abs(sum1 + v[n / 2] - (sum2 - v[n / 2]));
		cout << "1 " << max(sum_a,sum_b);
	}
	return 0;
}

你可能感兴趣的:(PAT甲)