題目連結:http://www.topcoder.com/stat?c=problem_statement&pm=10361
解題思路:
最開始我想通過對砍掉的樹數從小到大進行枚舉,最大可以達到2^40,枚舉量太大了。
改變思路,通過枚舉fence的大小,對fence外的和內的分別做處理,豈不是一個很好的思路。為了減少時間,可以在枚舉前對tree按照height從大到小進行排序。同時枚舉的時候可以保證後面的邊比前面的大,也可以減少不少的枚舉量。最後本題開始我用了兩個函數outer()和inner()導致逾時,直接把判斷放到if裡面就可以了,my god!
最佳化後的:
最佳化後的:<br />#include<vector><br />#include <cmath><br />using namespace std;<br />struct SaveTheTrees<br />{<br />inline void swap(int &x,int &y)<br />{<br />int temp;<br />temp=x;x=y;y=temp;<br />}<br />inline bool outer(vector<int> x, vector<int> y,int num,int x1,int x2,int y1,int y2)<br />{<br />//if(x1<x2) swap(x1,x2);<br />//if(y1<y2) swap(y1,y2);<br />if ((x[num]>x1||x[num]<x2)||(y[num]>y1||y[num]<y2))<br />{<br />return true;<br />}<br />return false;<br />}<br />inline bool inner(vector<int> x, vector<int> y,int num,int x1,int x2,int y1,int y2)<br />{<br />//if(x1<x2) swap(x1,x2);<br />//if(y1<y2) swap(y1,y2);<br />if ((x[num]<=x1&&x[num]>=x2)&&(y[num]<=y1&&y[num]>=y2))<br />{<br />return true;<br />}<br />return false;<br />}<br />int minimumCut(vector<int> x, vector<int> y, vector<int> h)<br />{<br />int n=x.size();<br />//sort the array by the height<br />for (int i=0;i<n;i++)<br />{<br />for (int j=i+1;j<n;j++)<br />{<br />if (h[i]<h[j])<br />{<br />int temp;<br />temp=h[i];h[i]=h[j];h[j]=temp;<br />temp=x[i];x[i]=x[j];x[j]=temp;<br />temp=y[i];y[i]=y[j];y[j]=temp;<br />}<br />}<br />}<br />//enum the (x1,y1) (x2,y2) these are two vectexs on the rectangle<br />int x1,x2,y1,y2;<br /> int ans=n;<br />for (int i1=0;i1<n;i1++)<br />{<br />x1=x[i1];<br />for(int i2=0;i2<n;i2++)<br />{<br />x2=x[i2];<br />if (x1<x2) continue;<br />for (int j1=0;j1<n;j1++)<br />{<br />y1=y[j1];<br />for (int j2=0;j2<n;j2++)<br />{<br />y2=y[j2];<br />if(y1<y2) continue;<br /> int now=2*((x1-x2)+(y1-y2));<br />int fence=0;<br />int count=0;<br />//enum the trees outside the fence<br />for (int ii=0;ii<n;ii++)<br />{<br />if (outer(x,y,ii,x1,x2,y1,y2)==true)<br />{<br />fence+=h[ii];<br />count++;<br />}<br />}<br />//enum the trees needed inside the fence<br />for (int jj=0;jj<n&&fence<now;jj++)<br />{<br />if(inner(x,y,jj,x1,x2,y1,y2)==true)<br />{<br />fence+=h[jj];<br />count++;<br />}<br />}<br />//adjust the ans<br />if (count<ans)<br />{<br />ans = count;<br />}<br />}<br />}<br />}<br />}<br />return ans;<br />}<br />};<br />
帶函數,逾時!!!
#include<vector><br />#include <cmath><br />using namespace std;<br />struct SaveTheTrees<br />{<br />int minimumCut(vector<int> x, vector<int> y, vector<int> h)<br />{<br />int n=x.size();<br />//sort the array by the height<br />for (int i=0;i<n;i++)<br />{<br />for (int j=i+1;j<n;j++)<br />{<br />if (h[i]<h[j])<br />{<br />int temp;<br />temp=h[i];h[i]=h[j];h[j]=temp;<br />temp=x[i];x[i]=x[j];x[j]=temp;<br />temp=y[i];y[i]=y[j];y[j]=temp;<br />}<br />}<br />}<br />//enum the (x1,y1) (x2,y2) these are two vectexs on the rectangle<br />int x1,x2,y1,y2;<br /> int ans=n;<br />for (int i1=0;i1<n;i1++)<br />{<br />x1=x[i1];<br />for(int i2=0;i2<n;i2++)<br />{<br />x2=x[i2];<br />if (x1<x2) continue;<br />for (int j1=0;j1<n;j1++)<br />{<br />y1=y[j1];<br />for (int j2=0;j2<n;j2++)<br />{<br />y2=y[j2];<br />if(y1<y2) continue;<br />int now=2*((x1-x2)+(y1-y2));<br />int fence=0;<br />int count=0;<br />//enum the trees outside the fence<br />for (int ii=0;ii<n;ii++)<br />{<br />if ((x[ii]>x1||x[ii]<x2)||(y[ii]>y1||y[ii]<y2))<br />{<br />fence+=h[ii];<br />count++;<br />}<br />}<br />//enum the trees needed inside the fence<br />for (int jj=0;jj<n&&fence<now;jj++)<br />{<br />if((x[jj]<=x1&&x[jj]>=x2)&&(y[jj]<=y1&&y[jj]>=y2))<br />{<br />fence+=h[jj];<br />count++;<br />}<br />}<br />//adjust the ans<br />if (count<ans)<br />{<br />ans = count;<br />}<br />}<br />}<br />}<br />}<br />return ans;<br />}<br />};<br />