USACO Solution Code(6):Mixing Milk & Barn Repair

/*

ID: fairyroad

PROG: milk

LANG: C++

*/

 

#include<fstream>

#include <vector>

#include <algorithm>

using namespace std;

 

ifstream fin("milk.in");

ofstream fout("milk.out");

 

struct pack {

               int purchase;

               int amout;

               pack(int p, int a):purchase(p),amout(a){}

};

 

inline bool myComp(const pack& item1, const pack& item2 ){ return item1.purchase<item2.purchase; }

 

int main()

{

               int sum=0, num;

               fin>>sum>>num;

               vector<pack>  myPack;

               int i=0;

               while (i<num){

                               int purchase, amout;

                               fin>>purchase>>amout;

                               myPack.push_back(pack(purchase, amout));

                               ++i;

               }

 

               sort(myPack.begin(), myPack.end(), myComp);

              

               int res=0;

               for (int i=0;i<num;++i)

               {

                               int tmp=sum-myPack[i].amout;

                               if (tmp>0){

                                              sum=tmp;

                                              res+=myPack[i].purchase*myPack[i].amout;

                               }

                               else{

                                              res+=myPack[i].purchase*sum;

                                              break;

                               }

               }

 

               fout<<res<<endl;

               return 0;

}

 

 

/*

ID: fairyroad

PROG: barn1

LANG: C++

*/

 

#include<fstream>

#include<vector>

#include<algorithm>

using namespace std;

 

ofstream fout ("barn1.out");

ifstream fin ("barn1.in");

 

struct unit{

               size_t start, spaceLen;

// respectively represents the starting point and the length of the coming space

               unit(size_t s, size_t l) : start(s), spaceLen(l) {}

};

 

inline bool myComp(const unit& item1, const unit& item2) { return item1.spaceLen<item2.spaceLen; }

 

int main()

{

               size_t boardmax, barnnum, cownum;

               fin>>boardmax>>barnnum>>cownum;

 

               vector<bool> ID(barnnum, false);

               int id;

               for (size_t i=0;i<barnnum; i++)  { fin>>id; ID[id-1]=true;}

 

               vector<unit> barn;

               size_t patrol=0, remainder=0;

               while (patrol<barnnum)

               {

                               if (!ID[patrol]){

                                              size_t spacelen=0;

                                              while(patrol<barnnum && !ID[patrol]) { ++spacelen; ++patrol; }

                                              barn.push_back(unit(patrol-spacelen, spacelen));

                               }

                               else ++patrol;

               }

               if(!ID[0]) ++remainder;

               if(!ID[barnnum-1]) ++remainder;

 

               sort(barn.begin(), barn.end(), myComp);

 

               size_t currSpace=barn.size(), tmp=0;

               while (currSpace>=boardmax+remainder)

               {

                               if (barn[tmp].start!=0 && barn[tmp].start+barn[tmp].spaceLen<barnnum) {

                                              for (size_t i=0; i<barn[tmp].spaceLen;++i)

                                                             ID[i+barn[tmp].start]=true;

                                              --currSpace;

                               }

                               ++tmp;

               }

 

               int res=0;

               for (size_t i=0;i<barnnum; i++) res+= ID[i]?1:0;

 

               fout<<res<<endl;

               return 0;

}

 

你可能感兴趣的:(c,struct)