Topic:
The table has n legs, and when the table is unbalanced, it can be cut off to achieve maximum balance. The so-called maximum balance state refers to-the longest leg of the table is more than half the number of legs of the table. But the cost of cutting the legs of the table is different, and the cost of achieving the maximum balance is minimal.
Input:
6
2 2 1 1 3 3
4 3 5 5 2 1
Output:
8
The following is the last night's own idea, time is too short to tune out, today and perfected:
#include <string>#include<vector>#include<Set>#include<map>#include<cctype>#include<iostream>#include<string>using namespacestd;intMain () {intN; while(cin>>N) {vector<int> L (N,0); Vector<int> d (N,0); intI=0; while(i++<N) Cin>>l[i-1]; I=0; while(i++<N) Cin>>d[i-1]; I=0; if(n==1) {cout<<0<<Endl; Continue; } Map<int,multiset<int>>Res; for(intI=0; I<l.size (); i++) { if(Res.find (l[i]) = =Res.end ()) {Multiset<int>tmp; Tmp.insert (D[i]); Pair<int,multiset<int>>temp (l[i],tmp); Res.insert (temp); } Else{Res[l[i]].insert (d[i]); }} multiset<int>Cost ; Map<int,multiset<int>>::iterator res_i=Res.begin (); Map<int,multiset<int>>:: Iterator Res_j; Map<int,multiset<int>>:: Iterator Res_k; for(; Res_i!=res.end (); res_i++) { intsum=0;//Total cost per time intcount=n-(*res_i). Second.size ();//total number of legs per table++res_i; for(Res_j=res_i;res_j!=res.end (); res_j++)//long legs behind.{Count-=(*res_j). Second.size (); for(multiset<int>::iterator m= (*res_j). Second.begin (); m!= (*res_j). Second.end (); m++) {sum+=*m; }} res_i--; if((*res_i). Second.size () >count) {Cost.insert (sum); Continue; } Else{multiset<int>before_d; for(Res_k=res.begin (); res_k!=res_i;res_k++) { for(multiset<int>::iterator m= (*res_k). Second.begin (); m!= (*res_k). Second.end (); m++) {Before_d.insert (*m); }} multiset<int>::iterator it=Before_d.begin (); for(Count;count> (*res_i). Second.size ()-1; Count--, it++) {sum+=*it; } cost.insert (sum); }} cout<<* (Cost.begin ()) <<Endl; } return 0;}
Above is the use of map to achieve two elements of the arrangement.
The following is the use of defining classes for encapsulation, as well as overloaded functions:
#include <string>#include<vector>#include<Set>#include<map>#include<cctype>#include<iostream>#include<string>#include<algorithm>using namespacestd;classleg{ Public: intlength; intCost ; Leg (intXinty): Length (x), cost (y) {}BOOL operator< (Constleg& a)Const //No, const will be an error. { returnlength<a.length; } BOOL operator=(Constleg& a)Const { returnlength==a.length; }};classleg_cost{ Public: intlength; intCost ; Leg_cost (intXinty): Length (x), cost (y) {}BOOL operator< (Constleg_cost& a)Const { returncost<A.cost; }};intMain () {intN; while(cin>>N) {multiset<Leg>Res; Set<int>Len; Vector<int>l (N,0); intI=0; while(I++ <N) {cin>>l[i-1]; Len.insert (L[i-1]); } I=0; while(I++ <N) {inttmp; CIN>>tmp; Res.insert (Leg (l[i-1],tmp)); } pair<multiset<leg>::iterator,multiset<leg>::iterator>Max; Multiset<int>Min_cost; for(Set<int>::iterator Cur=len.begin (); Cur!=len.end (); cur++) { intCount=n-res.count (Leg (*cur,0)); intsum=0; Max=res.equal_range (Leg (*cur,0));//[) before the definition of <, also compared the cost, and later found that Equal_range's internal implementation function is <, so the cost comparison is canceled. for(Multiset<leg>::iterator after=max.second;after!=res.end (); after++) {sum+=after->Cost ; Count--; } if(Res.count (Leg (*cur,0)) >count) {Min_cost.insert (sum); Continue; } Else{multiset<Leg_cost>Del_cost; Multiset<Leg_cost>:: Iterator it; for(Multiset<leg>::iterator before=res.begin (); before!=max.first;before++) {Del_cost.insert (Leg_cost (Before->length,before->Cost )); } for(It=del_cost.begin (); Count>res.count (Leg (*cur,0))-1; count--) {sum+=it->Cost ; } min_cost.insert (sum); }} cout<<* (Min_cost.begin ()) <<Endl; } return 0;}
March 31: Mushroom Street Internship test: The cost of finding the maximum balance of the table