非常有趣的一件事是今天在TopCoder的1000分题里面发现了这道经典数学问题。
Notes | ||||||
- | In an optimal solution, exactly two people will be sent across the bridge with the flashlight each time (if possible), and exactly one person will be sent back with the flashlight each time. In other words, in an optimal solution, you will never send more than one person back from the far side at a time, and you will never send less than two people across to the far side each time (when possible). | |||||
Constraints | ||||||
- | times will have between 1 and 6 elements, inclusive. | |||||
- | Each element of times will be between 1 and 100, inclusive. | |||||
Examples | ||||||
0) | ||||||
| ||||||
1) | ||||||
| ||||||
2) | ||||||
| ||||||
3) | ||||||
|
计算的这道题方法其实类似于动态规划,关键在于寻找最优子结构
1)问题的最优子结构是这样推出的
1.每一个人都得过河
2.由1可以知道cost最大的一个也必须过河
3.由2可知必然有一次过河的代价为cost(max)
4.由3可知,在将cost最大的人送过河的运输中最优的方案是将cost第二大的人也同时过河
因此问题可以转化为如何将cost第一大和第二大的两个人同时送过河
2)最优化问题的解法在于首先将cost最小的两个人先送过河然后选择其一送回手电筒(无论哪个人都一样),然后再使cost最大和第二大的两个人同时过河,再另上一次剩在另一 岸的cost最小或者次小的人送回手电筒
因此每次将一对人送过河的cost=iMax1st+(iMin2nd+2*iMin1st)
3)按总人数的奇数偶数可以将整个问题循环之后分支为两个子问题(显而易见,不多赘述)
4)利用大根堆和小根堆使遍历的时间复杂度从n降低至logn
多次实验后代码如下(以下大部分是大根堆小根堆的搭建代码):
#include<set>
#include<vector>
#include<set>
#include<vector>
#include<iostream> using namespace std; #define HEAP_SIZE 1024
//////////////////////////////////////////
template<typename T>
class MaxHeap{
T*arrData;
int top;
////////////Filters Up&Down void swap(T*l,T*r){
T temp=*l;
*l=*r;
*r=temp;
} //////////////////////////////////////////
void FilterUp(){
int kid=top-;
int parent=(kid-)/;
while(arrData[kid]>arrData[parent]){
swap(&arrData[kid],&arrData[parent]);
kid=parent;
parent=(parent-)/;
}
}
/////////////////////////////////////////
void FilterDown(){
int parent=;
int kid=arrData[parent*+]>arrData[parent*+]?parent*+:parent*+;
while(kid<top&&arrData[kid]>arrData[parent]){
swap(&arrData[kid],&arrData[parent]);
parent=kid;
kid=arrData[parent*+]>arrData[parent*+]?parent*+:parent*+;
}
}
public:
MaxHeap():top(){ arrData=new T[HEAP_SIZE]; }
~MaxHeap(){ delete arrData; } /////////////////////////////////////////////
////// Constractor & Destructor Above
/////////////////////////////////////////////
bool insert(T v){
if(top<HEAP_SIZE){
arrData[top]=v;
top++;
FilterUp();
return true;
}else return false;
} T remove(){
if(top){
int iTop=arrData[];
arrData[]=arrData[top-];
top--;
FilterDown();
return iTop;
}else return ;
}
}; //////////////////////////////////
template<typename T>
class MinHeap{
T*arrData;
int top;
////////////Filters Up&Down void swap(T*l,T*r){
T temp=*l;
*l=*r;
*r=temp;
} //////////////////////////////////////////
void FilterUp(){
int kid=top-;
int parent=(kid-)/;
while(arrData[kid]<arrData[parent]){
swap(&arrData[kid],&arrData[parent]);
kid=parent;
parent=(parent-)/;
}
}
/////////////////////////////////////////
void FilterDown(){
int parent=;
int kid=arrData[parent*+]<arrData[parent*+]?parent*+:parent*+;
while(kid<top&&arrData[kid]<arrData[parent]){
swap(&arrData[kid],&arrData[parent]);
parent=kid;
kid=arrData[parent*+]<arrData[parent*+]?parent*+:parent*+;
}
}
//////////////////////////////////////////////////////
public:
MinHeap():top(){ arrData=new T[HEAP_SIZE]; }
~MinHeap(){ delete arrData; } /////////////////////////////////////////////
////// Constractor & Destructor Above
///////////////////////////////////////////// bool insert(T v){
if(top<HEAP_SIZE){
arrData[top]=v;
top++;
FilterUp();
return true;
}else return false;
} T remove(){
if(top){
int iTop=arrData[];
arrData[]=arrData[top-];
top--;
FilterDown();
return iTop;
}else return ;
}
}; class BridgeCrossing{ void init(vector<int> v){
for(int i=;i<v.size();i++){
here.insert(v[i]);
here2.insert(v[i]);
}
} public:
MinHeap<int> here;
MinHeap<int> there;
MaxHeap<int> here2;
int minTime(vector<int> times){
int iTotal=;
init(times);
int max1st=-;
int max2nd=-;
int min1st=here.remove();
int min2nd=here.remove();
//////Returning Back
there.insert(min1st);
here.insert(min2nd);
while(true){
max1st=here2.remove();
if(max1st==min2nd)break;
max2nd=here2.remove();
if(max2nd==min2nd)break;
iTotal+=max1st+min1st+*min2nd;
}
if(max1st==min2nd){
iTotal+=min2nd;
return iTotal;
}else if(max2nd==min2nd){
iTotal+=min2nd+min1st+max1st;
return iTotal;
}else return -;
}
};