1409B - Minimum Product - CodeForces Solution


brute force greedy math *1100

Please click on ads to support us..

Python Code:

def solve(min_v, max_v, min_limit, max_limit, n):
    prev = min_v
    min_v = max(min_limit, min_v - n)
    n -= prev - min_v

    max_v = max(max_limit, max_v - n)
    return max_v*min_v



def main():
    t = int(input())
    for _ in range(t):
        a, b, x, y, n = map(int, input().split())
        if a >= b:
            min_v = b
            min_limit = y
            max_v = a
            max_limit = x
        else:
            min_v = a
            min_limit = x
            max_v = b
            max_limit = y

        m1 = solve(min_v, max_v, min_limit, max_limit, n)
        m2 = solve(max_v, min_v, max_limit, min_limit, n)


        print(min(m1, m2))


main()

C++ Code:

        #include <bits/stdc++.h>
        #pragma OCC optimize "trapv"



        using namespace std;
        typedef long long ll;
        typedef int tt;
        typedef unsigned long long ull;
        #define yes cout<<"YES"<<'\n'
        #define no cout<<"NO"<<'\n'
        #define output(v) for(auto&it:v){cout<<it<<" ";}cout<<"\n"
        #define input(v) for(auto&it:v){cin>>it;}
        #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

        #define output(v) for(auto&it:v){cout<<it<<" ";}cout<<"\n"
        #define input(v) for(auto&it:v){cin>>it;}
        typedef long long ll;
        void FastCode()
        {
            std::ios_base::sync_with_stdio(0);
            cin.tie(NULL);
            cout.tie(NULL);
        }
        void File(){
            freopen("popcorn.in","r",stdin);
            //  freopen("output.txt","w",stdout);
        }
        #include <bits/stdc++.h>
        using namespace std;

        #define output(v) for(auto&it:v){cout<<it<<" ";}cout<<"\n"
        #define input(v) for(auto&it:v){cin>>it;}
        int fact(int n);
        int nCr(int n, int r)
        {
            return fact(n) / (fact(r) * fact(n - r));
        }
        int fact(int n)
        {
            if(n==0)
                return 1;
            int res = 1;
            for (int i = 2; i <= n; i++)
                res = res * i;
            return res;
        }
        int power(int b , int p)
        {
            if(p == 1)
            {
                return b;
            }
            else if(p == 0)
            {
                return 1;
            }
            else
            {
                return b*power(b,p-1);
            }

        }
        const int MAX=1e6;
        const int N=1e5+5;

        int sumNatural(int n)
        {
            int sum = (n * (n + 1)) / 2;
            return sum;
        }
        void testcase() {
        }
        bool isDis(int num){
            string number = to_string(num);
            int arr[10]={};
            for (int i = 0; i < number.size(); ++i)
                if(arr[number[i]-'0']==0){
                    arr[number[i]-'0']++;
                }else return false;
            return true;
        }
        bool isPrime( long long n)
        {
            if( n==1) return false;
            else if( n==2) return true;
            else
            {
                for( int i=2; i<=sqrt( n+0.5); ++i)
                    if( n%i==0) return false;

                return true;
            }
        }
        int reverseDigits(int num)
        {
            int rev_num = 0;
            while (num > 0) {
                rev_num = rev_num * 10 + num % 10;
                num = num / 10;
            }
            return rev_num;
        }
        void swapa(int n1,int n2)
        {
            cout<<n2<<" "<<n1;
        }
        void print(long long n){
            ll arr[n];
            for (int i = 0; i <n ; ++i) {
                cin>>arr[i];
            }
            sort(arr,arr+n);
            cout<<arr[0]<<" "<<arr[n-1];
        }

        ll A[100008];
        ll B[100008];
        int C[100008];
        // char arr[109][109];
        ll gcd(ll a , ll b){
            if(min(a,b) == 0) return max(a,b);
            else return gcd(b , a%b);
        }
        ll lcm(ll a , ll b){
            return a / gcd(a,b) * b;
        }
        void sum_digits(ll n) {


        }
        string removeZero(string str)
        {
            // Count leading zeros
            int i = 0;
            while (str[i] == '0')
                i++;

            // The erase function removes i characters
            // from given index (0 here)
            str.erase(0, i);

            return str;
        }
        ll removeZ(ll n){
            string s= to_string(n);
            string sZ="";
            for (int i = 0; i < s.size(); ++i) {
                if(s[i]!='0')
                    sZ+=s[i];
            }
            return stoi(sZ);
        }

        void solve() {
            int n;
            cin >> n;
            n /= 2;
            if (n & 1) {
                cout << "NO\n";
                return;
            }
            cout << "YES\n";
            ll sum = 0;
            for (int i = 1; i <= n; i++) {
                cout << 2 * i << ' ';
                sum += 2 * i;
            }
            for (int i = 1; i < n; i++) {
                sum -= 2 * i - 1;
                cout << 2 * i - 1 << ' ';
            }
            cout << sum << endl;
        }
        void reverseStr(string& str)
        {
            int n = str.length();

            // Swap character starting from two
            // corners
            for (int i = 0; i < n / 2; i++)
                swap(str[i], str[n - i - 1]);

        }
        int facts[100005];

        int beauty(int p[]) {
            int n;
            int res = 0;
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (p[i] > p[j]) res++;
                }
            }
            return res;
        }

        void permute(int p[], int i) {
            int n;
            if (i == n) {
                cout << beauty(p) << endl;
                return;
            }
            for (int j = i; j < n; j++) {
                swap(p[i], p[j]);
                permute(p, i + 1);
                swap(p[i], p[j]);
            }
        }
        int     ar1[100008],ar2[100008],ar3[100008];
        void answer(ll t) {

        }
        void testCase() {
            char ch;
            cin>>ch;
            string s="codeforces";
            for (int i = 0; i < s.size(); ++i) {
                if(ch==s[i]){
                    yes;
                    return;
                }
            }
            no;
        }
        void PrintReverseOrder(int N)
        {
            // if N is less than 1
            // then return void function
            if (N <= 0) {
                return;
            }
            else {
                cout << N << " ";

                // recursive call of the function
                PrintReverseOrder(N - 1);
            }
        }
        void print_recursion(int n) {
            if (n <= 0) return;
            std::cout << "I love Recursion" <<endl;
            print_recursion(n-1);
        }
        ll sol(ll a,ll b,ll x,ll y,ll n){
            ll temp1=min(n,a-x);
            a-=temp1;
            n-=temp1;
            temp1=min(b-y,n);
            b-=temp1;
            return a*b;
        }
        int main() {
            ll t;
            cin>>t;
            while (t--) {
                ll a, b, x, y, n;
                cin >> a >> b >> x >> y >> n;
                cout<<min(sol(a,b,x,y,n),sol(b,a,y,x,n))<<endl;
            }
        }


Comments

Submit
0 Comments
More Questions

466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume
1710C - XOR Triangle
415C - Mashmokh and Numbers
8A - Train and Peter
591A - Wizards' Duel
1703G - Good Key Bad Key
1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers