2017-08-25 12:00:53
writer:pprp
很简单的枚举,但是我调试了很长时间,出现各种各样的问题
/* theme:cf 589B writer:pprp declare:枚举 date:2017/8/25 */ #include <bits/stdc++.h> using namespace std; const int N = 4040; typedef long long ll; ll ans = -1, record_w = -1, record_h = -1; class rect { public: int w; int h; bool operator <(const rect & r2) { return w < r2.w; } }; rect rec[N]; int main() { int n; scanf("%d",&n); //input section for(int i = 0 ; i < n ; i++) { scanf("%d%d",&rec[i].w, &rec[i].h); //w is bigger than h if(rec[i].w > rec[i].h) //w > h?? swap(rec[i].w,rec[i].h); } //sort the w sort(rec,rec + n); //define a vector to store the height vector<int> hh; //从小到大枚举w的长度 for(int i = 0 ; i < n ; i++) { hh.clear(); //将宽度高于w的对象的h储存在vector中 for(int j = i ; j < n ; j++) hh.push_back(rec[j].h);//一开始这里写成i了粗心犯的错 //对高度进行排序 sort(hh.begin(), hh.end()); //记录当前高度 int len = hh.size(); //枚举当前w的情况下,采用不同的h的最佳解 for(int j = 0 ; j < hh.size() ; j++, len--) { ll cmp = (ll)rec[i].w * hh[j] * len; //wrong before: (ll)(rec[i].w * hh[j] * len) 这样就会越界,这个错误是调试出来的,如果都是ll就会溢出 if(cmp > ans) { ans = cmp; record_h = hh[j]; record_w = rec[i].w; } } } cout << ans << endl; cout << record_w << " " << record_h << endl; return 0; }