USACO Solution Code(3):Milking Cows

比較簡單的題,不過這個題的模型很有普適性,很多涉及區間的操作其實是編程中很常見的一類問題。第一次提交的代碼用到了STLbitset,每讀取一個時間段,就將該時間段塗色——即置“1”。若本着運行效率至上的原則,代碼很多值得優化的地方,比如“塗色(tintage)”這種處理方法就不是最好的,直接用(start,end)序對來表示一個區間,然後進行區間的插入或合併操作,於空間和時間都是更好的辦法。不過編碼很比較複雜(看第二版本代碼裏那些個if-else)就知道了,呵呵,我始終認爲,在複雜度可以接受的範圍內,應該把代碼的清晰、簡明放在第一位,然後纔是時空效率上的優化。不過我爲了滿足追求效率的老大們的需求,俺也貼上區間插入/合併版的代碼吧,呵呵~

 

/*

ID: fairyroad

PROG: milk2

LANG: C++

*/

#include<fstream>

#include <bitset>

using namespace std;

 

const int LEN=1000001;

bitset<LEN> myColl;

 

inline void tintage(int start, int end){

    while(start<end) myColl.set(start++);

}

 

int main()

{

               ofstream fout ("milk2.out");

               ifstream fin ("milk2.in");

 

               int num;

               fin>>num;

 

               int header=LEN, tail=0;

               int start, end;

 

               while(num)

               {

                   fin>>start>>end;

                   header=header<start ? header:start;

                   tail=tail>end ? tail:end;

                   tintage(start, end);

                   --num;

               }

 

               int continuation=0 , interval=0, pos=header;

    while(pos<tail)

    {

        int tmpCon=0, tmpInter=0;

 

        while(pos<tail && myColl[pos]) { tmpCon++; pos++;}

        while(pos<tail && !myColl[pos]) { tmpInter++; pos++;}

 

        continuation=continuation>tmpCon?continuation:tmpCon;

        interval=interval>tmpInter?interval:tmpInter;

    }

               fout<<continuation<<" "<<interval<<endl;

               return 0;

}

 

下面是區間使用處理算法的代碼:

 

/*

ID: fairyroad

PROG: milk2

LANG: C++

*/

#include<fstream>

#include<vector>

#include<algorithm>

using namespace std;

 

typedef pair<int, int> segment;

typedef vector<segment> segVec;

typedef segVec::iterator segIter;

segVec coll;

 

inline bool myComp(segment s1, segment s2) {

               return s1.first<s2.first ;

}

 

int main()

{

               ofstream fout ("milk2.out");

               ifstream fin ("milk2.in");

 

               int num;

               fin>>num;

 

               int start, end;

               while(num){

                               fin>>start>>end;

                               coll.push_back(make_pair(start,end));

                               --num;

               }

 

               segIter header=coll.begin(), tail=coll.end();

 

               sort(header, tail, myComp);

              

               int continuation=header->second-header->first, interval=0;

               if (coll.size()==1){

                               fout<<continuation<<" "<<interval<<endl;

                               return 0;

               }

 

               segIter current=header;

               while(current!= --coll.end())

               {

                               segIter next=current+1;

                               if (current->second>=next->first) // merge the two segment

                               {

                                              if (next->second>current->second){

                                                             current->second=next->second;

                                                             int temp1=current->second-current->first;

                                                             continuation=temp1>continuation ? temp1:continuation;

                                              }

                                              coll.erase(next);

                               }

                               else{

                                              int temp2=next->first-current->second;

                                              interval=temp2>interval ? temp2:interval;

                                              int temp3=next->second-next->first;

                                              continuation=temp3>continuation ? temp3:continuation;

                                              ++current;

                               }

               }

 

               fout<<continuation<<" "<<interval<<endl;

               return 0;

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章