Overcome ACM POJ 2370 - Democracy in danger

鍾超發表於2012-02-16

Problem:2370
User:Poechant
Memory:728K
Time:16MS
Language:G++
Result:Accepted

/*

* 作者:柳大·Poechant

* 部落格:http://blog.csdn.net/poechant

* 思路: 先按每組人數對n組進行快排,然後算出前(n/2+1)組各組人數(若為x)的x/2+1的和,即為結果。

*/

#include<iostream>

usingnamespace std;


int partition(int* data,int l,int h);

void quick_sort(int* data,int l,int h);


int main(void)

{

int n;

while (cin >> n)

{

int i;

int *m =newint[n];

int sum = 0;

for (i = 0; i < n; ++i)

{

cin >> m[i];

sum += m[i];

}

quick_sort(m, 0, n -1);


int d = 0;

i = 0;

int l = n / 2 + 1;

while (i < l)

{

d += m[i] / 2 + 1;

++i;

}

cout << d <<endl;

}

return 0;

}


// quick sort algorithm

void quick_sort(int* data,int l,int h)

{

if (l < h)

{

int m = partition(data, l, h);

quick_sort(data, l, m - 1);

quick_sort(data, m + 1, h);

}

}


// partition for quick sorting

int partition(int* data,int l,int h)

{

int k = data[h];

int j = l;

for (int i = l; i <= h; ++i)

{

if (data[i] < k)

{

data[i] = data[i] + data[j] - (data[j] = data[i]);

++j;

}

}

if (data[j] > k)

{

data[j] = data[j] + data[h] - (data[h] = data[j]);

}

elseif (j < h)

{

++j;

data[j] = data[j] + data[h] - (data[h] = data[j]);

}

return j;

}