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;i<b;i++){
c+=P[i];
}
return c;
}
int sumU(int a,int b){
int c=0;
for(int i=a;i<b;i++){
c+=U[i];
}
return c;
}
int main(){
int t;
scanf(“%d”,&t);
for(int u=0;u<t;u++){
int a,b,c,m,len1=0,len2=0,x,cnt=0,mon=0;
scanf(“%d%d%d%d”,&a,&b,&c,&m);
char s[5];
for(int y=0;y<m;y++){
scanf(“%d”,&x);
scanf(“%s”,s);
if(s[0]==’U’) U[len1++]=x;
else if(s[0]==’P’) P[len2++]=x;
}
sort(U,U+len1);
sort(P,P+len2);
int lena=0,lenb=0;
if(a<len1){
cnt+=a;
lena=a;
mon+=sumU(0,a);
}
else{
cnt+=len1;
lena=len1;
mon+=sumU(0,len1);
}
if(b<len2){
cnt+=b;
lenb=b;
mon+=sumP(0,b);
}
else{
cnt+=len2;
lenb=len2;
mon+=sumP(0,len2);
}
while((lena0){ 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;
}

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