这道题关键在于有两种方式进行搬运;
first1 , first2 , 5 , 7 , last2 , last1
first1 = 3; first2 = 4; last1 = 10; last2 = 8;
第一种是:2人为搬运工
3、4一起过去;
3带回手电筒;
8、10一起过去;
4带回手电筒;
4 + 3 + 10 + 4 = 21;
第二种是:1人为搬运工
3、8一起过去;
3带回手电筒;
3、10一起过去;
3带回手电筒;
8 + 3 + 10 + 3 = 24;
只要判断哪个小就行了。
还有就是第一种方式中(5、7)(8、10)组合比
(5、10)(7、8)花的时间少,即不能乱序组合。
#include #include #include using namespace std; int main() { int n; vector p; cin >> n; int temp; while (n--) { cin >> temp; p.push_back(temp); } sort(p.begin(), p.end()); if (p.size() <= 2) { cout << p.back() << endl; return 0; } int first1 = p[0], first2 = p[1]; int last1, last2; int count = 0; while (p.size() >= 4) { last1 = p.back(); p.pop_back(); last2 = p.back(); p.pop_back(); if (first1 + last1 + first2 * 2 < last1 + last2 + first1 * 2) { count += first1 + last1 + first2 * 2; } else { count += last1 + last2 + first1 * 2; } } if (p.size() == 3) { count += first1 + first2 + p[3]; } else if (p.size() == 2) { count += first2; } cout << count << endl; system("pause"); }
// 输入 6 3 4 5 7 8 10 // 输出 43