brute force greedy implementation math *800

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    x=input()
    y=input()
    b=int(x.split()[0])
    p=int(x.split()[1])
    f=int(x.split()[2])
    h=int(y.split()[0])     c=int(y.split()[1])     h1=0     c1=0     if b>=(p+f)*2:
        h1=p
        c1=f
    elif h>=c and b>=p*2:
        h1=p
        c1=(b-p*2)//2
    elif c>h and b>=f*2:
        c1=f
        h1=(b-f*2)//2
    elif h>=c and b<p*2:
        h1=b//2
        c1=0
    elif c>h and b<f*2:
        c1=b//2
        h1=0
    print(h*h1+c*c1)

C++ Code:

#include <bits/stdc++.h> 
using namespace std;
int main () {
    long long a,b,c,d,l,m,p,f,o,i,j,r,q[1000000],k,s,t,e,x,y,z,h;
    char n[1000100],g[12345];
    cin>>a;
    while(a--) {
        cin>>b>>p>>f>>h>>c;
    x=0;
    y=0;
    b=(b-b%2)/2;
    if(h>c) {
        if(b>=p) {
            x=p*h;
            b=b-p;
            if(b>=f) {
                y=f*c;
            }
            else {
                y=b*c;
            }
        }
        else {
            x=b*h;
            y=0;
        }
    }
    else {
        if(b>=f) {
            y=f*c;
            b=b-f;
            if(b>=p) {
                x=p*h;
            }
            else {
                x=b*h;
            }
        }
        else {
            y=b*c;
            x=0;
        }
    }
    cout<<x+y<<endl;
    }
}


Comments

Submit
0 Comments
More Questions

1523B - Lord of the Values
1406C - Link Cut Centroids
2409. Count Days Spent Together
2410. Maximum Matching of Players With Trainers
1604C - Di-visible Confusion
997A - Convert to Ones
218A - Mountain Scenery
486B - OR in Matrix
1405A - Permutation Forgery
1733A - Consecutive Sum
1733B - Rule of League
1733C - Parity Shuffle Sorting
1264A - Beautiful Regional Contest
1695A - Subrectangle Guess
467B - Fedor and New Game
252C - Points on Line
735C - Tennis Championship
992A - Nastya and an Array
554A - Kyoya and Photobooks
79B - Colorful Field
265B - Roadside Trees (Simplified Edition)
1362C - Johnny and Another Rating Drop
1214C - Bad Sequence
1091B - New Year and the Treasure Geolocation
244A - Dividing Orange
1061C - Multiplicity
1312A - Two Regular Polygons
801A - Vicious Keyboard
510B - Fox And Two Dots
616D - Longest k-Good Segment