3193. 键盘

10175101268

求一个简单的做法

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;

}

CCXXXI_

简单的做法也就是尽量复用代码了(当然也和语言的选择有关,比如py一般比cpp写起来简单


from collections import defaultdict
for _ in range(int(input())):
    print(f'case #{_}:')
    price, num = defaultdict(list), dict()
    num['U'], num['P'], num['B'] = map(int, input().split())
    for _ in range(int(input())):
        p, k = input().split()
        price[k[0]].append(int(p))
    ans = [0, 0]

    # 这样给多少种键盘都能直接调用这一个函数解决,比每个单独计算就简单多了
    def calc(k):
        price[k].sort()
        cnt = min(num[k], len(price[k]))
        ans[0] += cnt
        ans[1] += sum(price[k][:cnt])
        del price[k][:cnt]

    calc('U')
    calc('P')
    price['B'] = price['U'] + price['P']
    calc('B')
    print(*ans)
970163114

感觉比较复杂的代码…

include

include

using namespace std;
int U[301],P[301];
int sumP(int a,int b){
int c=0;
for(int i=a;i0){
if(lena==len1){
mon+=P[lenb++];
cnt++;
}
else if(lenb==len2){
mon+=U[lena++];
cnt++;
}
else{
if(U[lena]>P[lenb]){
cnt++;
mon+=P[lenb++];
}
else{
cnt++;
mon+=U[lena++];
}
}
c–;
}
printf(“case #%d:\n”,u);
printf(“%d %d\n”,cnt,mon);
}
return 0;
}

你当前正在回复 博客/题目
存在问题!