POJ1700 Crossing River 過河問題
一、題目資訊
Crossing River
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8897 | Accepted: 3323 |
Description
A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people
may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for
each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.
Output
For each test case, print a line containing the total number of seconds required for all the N people to cross the river.
Sample Input
1 4 1 2 5 10
Sample Output
17
二、參考程式碼
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int N;
cin >> N;
while(N--)
{
vector<int> a;
int n,m,sum = 0;
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> m;
a.push_back(m);
}
sort(a.begin(),a.end());
a.insert(a.begin(),0);
while(1)
{
if(n == 2)
{
sum += a[2];
break;
}
else if(n == 1)
{
sum += a[1];
break;
}
else if(n == 3)
{
sum +=a[1] + a[2] + a[3];
break;
}
else
{
sum += min(2*a[2] + a[1] + a[n] ,a[n] + 2*a[1] + a[n-1]);
a.erase(a.end()-1,a.end());
n -= 2;
}
}
cout << sum << endl;
}
return 0;
}
相關文章
- BZOJ 1617 [Usaco2008 Mar]River Crossing渡河問題:dpROS
- ACM 過河問題ACM
- 演算法分析——青蛙過河問題演算法
- AI基礎:美女和野人過河問題AI
- 過河 手電筒 面試題面試題
- [原始碼和文件分享]基於JAVA實現的農夫過河問題原始碼Java
- Docker與銀河麒麟的相容性問題Docker
- 小馬過河-RPC之旅RPC
- 銀河麒麟系統安裝ORACLE資料庫問題處理Oracle資料庫
- Leetcode: Self CrossingLeetCodeROS
- Leetcode - Self CrossingLeetCodeROS
- LeLeetCode 403. 青蛙過河LeetCode
- LeetCode-Self CrossingLeetCodeROS
- 那些年,翻過山,趟過河,挖了山丘,黑了河溝,終於還是遇到了——跨伺服器查詢伺服器
- 儲存過程問題。。儲存過程
- 網頁過期問題網頁
- Session的過期問題Session
- 國產化之銀河麒麟.netcore3.1訪問https服務的兩個問題NetCoreHTTP
- NOIP2005過河[DP 狀態壓縮]
- sphinx查詢過濾問題
- 通過truss命令trace問題
- webService 介面方法過載問題Web
- 【LeetCode】Self Crossing(335)LeetCodeROS
- 透過addm分析io問題
- 通過addm分析io問題
- 關於過濾字元的問題字元
- 如何捕獲問題SQL解決過度CPU消耗的問題SQL
- 如何捕獲問題SQL解決過度CPU消耗問題 (zt)SQL
- 捕獲問題SQL解決過度CPU消耗問題-- 轉載SQL
- P10935 銀河 解題報告
- LeetCode 403 Frog Jump 青蛙過河 Javascirpt 解決方案LeetCodeJava
- 摸著石頭過河:知乎核心業務 Go 語言改造實踐Go
- cpu使用率過高問題(Java)Java
- JS 頁面載入過程問題JS
- 一個lua問題解決過程
- 一個儲存過程的問題!儲存過程
- oracle11 密碼過期問題Oracle密碼
- 解決超過會話數問題會話