Overcome ACM POJ 2370 - Democracy in danger
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;
}
相關文章
- ACM訓練方案-POJ題目分類ACM
- POJ 3436-ACM Computer Factory(最大流輸出路徑-Edmond-Karp演算法)ACM演算法
- POJ-1581 A Contesting Decision-模擬ACM比賽時的評分規則ACM
- visa被拒,power dominant, democracy and equality,國家和私人空間NaN
- ACM summaryACM
- ACM blockhousesACMBloC
- ACM +-字串ACM字串
- ACM之歌ACM
- ACM模板ACM
- 前端學習(2370):元件之間的通訊方式前端元件
- ACM Registration systemACM
- ACM A problem is easyACM
- ACM Coin TestACM
- ACM 素數ACM
- ACM Arithmetic ExpressionACMExpress
- ACM The Famous ClockACM
- ACM 對決ACM
- ACM 找點ACM
- ACM Greedy MouseACM
- ACM Color the fenceACM
- ACM 瘋牛ACM
- ACM 推桌子ACM
- ACM 笨小熊ACM
- ACM cigarettesACM
- ACM notesACM
- POJ 3461 kmpKMP
- poj3417
- acm訓練題ACM
- ACM BUYING FEEDACM
- ACM 一種排序ACM排序
- ACM 漢諾塔(三)ACM
- ACM spiral gridACM
- ACM 字母統計ACM
- ACM 字串替換ACM字串
- ACM Longest Repeated SequenceACM
- ACM 交換輸出ACM
- ACM 房間安排ACM
- ACM 取石子(七)ACM