(XL) Lt1-2
The problem is still not very difficult, but note the operator overloads so that sort, as well as the construction members, and the sort encounter vector iterator.
Wrote two copies of the code.
Attach your own common method first:
#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;Const intmaxn= ++5;structsoldier{intb; intJ; BOOL operator< (ConstSoldier &a)Const{ returnJ>A.J; }}; Soldier A[MAXN];intMain () {intN,kase=0; while(SCANF ("%d", &n) = =1&&N) { for(intI=0; i<n;i++) scanf ("%d%d",&a[i].b,&A[I].J); Sort (A,a+N); intans=0; inttot=0; for(intI=0; i<n;i++) {tot+=a[i].b; if(Tot+a[i].j>ans) ans=tot+A[I].J; } Kase++; cout<<" Case"<<kase<<": "; cout<<ans<<Endl; } return 0;}
Then attach the tall vector and other methods, from the original book Method:
#include <cstdio>#include<iostream>#include<vector>#include<algorithm>using namespacestd;structjob{intj,b; BOOL operator< (Constjob& x)Const{//operator overloaded format, do not forget the const modifier returnJ>X.J; }};intMain () {intN,b,j,kase=1; while(SCANF ("%d", &n) = =1&&N) {Vector<Job>v;//using an indefinite-length array for(intI=0; i<n;i++) {scanf ("%d%d",&b,&j); V.push_back (Job) {j,b}); //push_back (Job) {}} sort (V.begin (), V.end ()); //iterators ints=0; intans=0; for(intI=0; i<n;i++) {s+=v[i].b; Ans=max (ans,s+V[I].J); } printf ("Case %d:%d\n", kase++, ans); } return 0;}
uva11729--Algorithm Competition Introductory classic Training Guide