求一个简单的做法
include
include
include
using namespace std;
bool cmp(int i,int j)
{
return i < j;
}
int main()
{
int T = 0;
string u = “USB”;
cin >> T;
for(int i = 0;i < T;i++){
int temp = 0;
int a = 0,b = 0,c = 0,m = 0;
int price = 0;
string type[310];
int USB[310] = {0};
int countu = 0;
int countuu = 0;
int ps[310] = {0};
int countp = 0;
int countpp = 0;
int count = 0;
char space;
int left1 = 0,left2 = 0;
cin >> a >> b >> c >> m;
for(int j = 0;j < m;j++){
cin >> temp >> type[j];
//cout << u << type[j];
if(u.compare(type[j]) == 0){
USB[countu++] = temp;
}
else{
ps[countp++] = temp;
}
}
sort(USB,USB+countu,cmp);
sort(ps,ps+countp,cmp);
left1 = countu - a;
left2 = countp - b;
if(b <= countp){
countpp = b;
}
else{
countpp = countp;
}
for(int j = 0;j < countpp;j++){
price += ps[j];
}
count += countpp;
if(a <= countu){
countuu = a;
}
else{
countuu = countu;
}
for(int j = 0;j < countuu;j++){
price += USB[j];
}
count += countuu;
for(int k = 0;k < c;k++){
if(left1 > 0){
if(left2 > 0){
if(USB[a] <= ps[b]){
price += USB[a++];
left1--;
count++;
}
else{
price += ps[b++];
left2--;
count++;
}
}
else{
price += USB[a++];
left1--;
count++;
}
}
else{
if(left2 > 0){
price += ps[b++];
left2--;
count++;
}
else{
break;
}
}
}
cout << "case #" << i << ":" << endl << count << " " << price << endl;
}
return 0;
}
简单的做法也就是尽量复用代码了(当然也和语言的选择有关,比如py一般比cpp写起来简单