#14267: Why MLE???


10530826 (Starry Knight)

學校 : 臺北市立成功高級中學
編號 : 62083
來源 : [114.198.167.25]
最後登入時間 :
2020-11-14 10:15:59
d221. 10954 - Add All -- UVa10954 | From: [1.168.117.138] | 發表日期 : 2018-07-09 06:41

#include <deque>
#include <iostream>
#include <algorithm>

using namespace std;

int binary_search(deque<int> nums, int t, int hi, int lo)
{
if (hi == lo)
{
return hi;
}
int m = (hi + lo) / 2;
if (nums[m] <= t)
{
return binary_search(nums, t, hi, m + 1);
}
else
{
return binary_search(nums, t, m, lo);
}
}

int main()
{
int n;
while (cin >> n)
{
if (n == 0)
{
break;
}
deque<int> nums;
long long int cost = 0;
for (int i = 0, tmp = 0; i < n; i++)
{
cin >> tmp;
nums.push_back(tmp);
}
sort(nums.begin(), nums.begin() + n);

for (int i = 0, tmp; i < n - 1; i++)
{
tmp = nums[0] + nums[1];
cost += tmp;
nums.pop_front();
nums.pop_front();
deque<int>::iterator it;
int k = binary_search(nums, tmp, nums.size(), 0);
it = nums.begin() + k;
it = nums.insert(it, tmp);
}
printf("%d\n", cost);
}
return 0;
}

 
ZeroJudge Forum