问题描述:
有n个部下,第i个需要花Bi时间交代任务,他需要花费Ji分钟完成.请选择交代任务的顺序,是所有任务完成是时间最短(不能同时给两个部下交代任务,但他们可以同时执行各自的任务)
基本思路:对各个部下执行任务时间按照非升序排列,如果上次执行任务与此次交代任务的剩余时间大于本次执行时间,就更新本次任务的执行时间.
My Code:
#include<iostream> #include<vector> #include<algorithm> using namespace std; const int maxn = 1000 + 5; class People { public: People() { tell = 0; excute = 0; } People(int tell, int excute) { this->tell = tell; this->excute = excute; } bool operator < (const People& x) const //运算符重载 { return excute > x.excute; } //private: int tell; int excute; }; int main() { int sum(vector<People> v); //void out(vector<People> v); int n, tell, excute, i_case = 1; while(cin >> n, n) { vector<People> v; for(int i = 0; i < n; ++i) { cin >> tell >> excute; v.push_back((People) { tell, excute }); //v(tell, excute); //v.push_back(v); } sort(v.begin(), v.end()); //out(v); cout << "Case " << i_case << ": "; cout << sum(v) << endl; ++i_case; } } int sum(vector<People> v) { int cost = v[0].tell; int remain = 0; for(int i = 1; i < v.size(); ++i) { cost += v[i].tell; remain = v[i-1].excute - v[i].tell; //重叠后剩余时间 if(remain > v[i].excute) v[i].excute = remain; //如果剩余时间大则更新本次执行时间 } cost += v[v.size()-1].excute; //加上最后一个执行时间 return cost; } /* void out(vector<People> v) { for(int i = 0; i < v.size(); ++i) cout << v[i].tell << " " << v[i].excute << endl; cout << endl; }
代码还有不足之处,例如应把vector定义在循环之外,但每次循环结束后都应该立即清空容器内容v.clear()等,养成良好的写作风格……
问题:
思维语言的转化还不够精炼,多参考经典代码,精炼抽象、多角度看问题。
勤加练习,熟能生巧。
任务:复习操作符重载内容……
Optimization:
#include<iostream> #include<vector> #include<algorithm> using namespace std; const int maxn = 1000 + 5; class People { public: People() { tell = 0; excute = 0; } People(int tell, int excute) { this->tell = tell; this->excute = excute; } bool operator < (const People& x) const //运算符重载 { return excute > x.excute; } //IO操作符必须为非成员函数,定义为类的友元函数实现调用私有成员。 friend istream& operator >> (istream& in, People& p); friend ostream& operator << (ostream& os, const People& p); //private: int tell; int excute; }; istream& operator >> (istream& in, People& p) { in >> p.tell >> p.excute; if(!in) p = People(); return in; } ostream& operator << (ostream& out, const People& p) { out << p.tell << " " << p.excute; return out; } int main() { int sum(vector<People> v); //void out(vector<People> v); int n, i_case = 1; vector<People> v; People p; while(cin >> n, n) { for(int i = 0; i < n; ++i) { cin >> p; v.push_back(p); } sort(v.begin(), v.end()); //out(v); cout << "Case " << i_case << ": "; cout << sum(v) << endl; ++i_case; v.clear(); } return 0; } int sum(vector<People> v) { int cost = v[0].tell; int remain = 0; for(int i = 1; i < v.size(); ++i) { cost += v[i].tell; remain = v[i-1].excute - v[i].tell; //重叠后剩余时间 if(remain > v[i].excute) v[i].excute = remain; //如果剩余时间大则更新本次执行时间 } cost += v[v.size()-1].excute; //加上最后一个执行时间 return cost; } void out(vector<People> v) { for(int i = 0; i < v.size(); ++i) cout << v[i] << endl; cout << endl; }
Reference:
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct Job { int j, b; bool operator < (const Job& x) const { return j > x.j; } }; int main() { int n, b, j, i_case = 1; while(cin >> n, n) { vector<Job> v; for(int i = 0; i < n; ++i) { cin >> b >> j; v.push_back((Job){j,b}); } sort(v.begin(), v.end()); int s = 0; int ans = 0; for(int i = 0; i < n; ++i) { s += v[i].b; //当前任务的开始执行时间 ans = max(ans, s+v[i].j); //更新任务执行完毕时的最晚时间 } cout << "Case " << i_case++ << ": " << ans << endl; } return 0; }