USACO Solution Code(3):Milking Cows

来源:互联网 发布:网络舆情 事件举例 编辑:程序博客网 时间:2024/06/05 20:26

比较简单的题,不过这个题的模型很有普适性,很多涉及区间的操作其实是编程中很常见的一类问题。第一次提交的代码用到了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;

}

 

 

 

原创粉丝点击