Earning A Profit CodeChef Solution

Problem -Earning A Profit CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.
<<Previous CodeChef Problem Next Codechef Problem>>

Earning A Profit CodeChef Solution in C++14

#include <bits/stdc++.h> 
using namespace std;
int main (int argc, char *argv[])
{
    int t;
    cin >> t;
    while(t--){
        int n,a;
        cin >> n >> a;
        int c[n+1],d[n+1];
        c[0] = 0;
        d[0] = 0;
        for(int i = 0; i < n; i++){
            cin >> c[i] >> d[i];
        }
        int maxx = 0,coins,fuel,dmin,dmax,curr;
        for(int i = 0; i < n; i++){
            fuel = 0;
            dmin=d[i];
            dmax=d[i];
            for(int j = i; j < n; j++){
                coins = (j-i+1)*a;
                if(d[j]<dmin)
                    dmin=d[j];
                if(d[j]>dmax)
                    dmax=d[j];
                fuel+=c[j];

                curr=coins-fuel-(dmax-dmin)*(dmax-dmin);
                maxx=max(maxx,curr);
            }
        }
        cout << maxx << endl;
    }
    return 0;
}

Earning A Profit CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
		int tc = Integer.parseInt(br.readLine());
		while(tc-->0){
		    StringTokenizer st = new StringTokenizer(br.readLine());
		    int n = Integer.parseInt(st.nextToken());
		    int a = Integer.parseInt(st.nextToken());
		    int [] cost = new int[n];
		    int [] diff = new int[n];
		    int i =0;
		    while(i<n){
		        StringTokenizer temp = new StringTokenizer(br.readLine());
		        cost[i]=Integer.parseInt(temp.nextToken());
		        diff[i]=Integer.parseInt(temp.nextToken());
		        i++;
		    }
		    for(i =1;i<n;i++){
		        cost[i]+=cost[i-1];
		    }
		    int[][] arr = new int[n][n];
		    for(int l=0;l<n;l++){
		        int max = 0;
		        int min = 100000000;
		        for(int r=l;r<n;r++){
		           max = Math.max(max,diff[r]);
		           min = Math.min(min,diff[r]);
		           arr[l][r]=(max-min)*(max-min);
		        }
		    }
		  //  System.out.println(Arrays.deepToString(arr));
		    int max = 0;
		    for(int l=0;l<n;l++){
		        for(int r =l;r<n;r++){
		            int c =0;
		            if(l==0){
		                c = cost[r];
		            }else{
		                c = cost[r]-cost[l-1];
		            }
		            int val = (r-l+1)*a -c-arr[l][r];
		          //  System.out.println(val + "  "+ max);
		            max = Math.max(max,val);
		        }
		        
		    }
	        writer.write(max+"\n");
		}
		writer.flush();
		writer.close();
		br.close();
	}
	
	static int gcd(int a, int b){
        return b == 0 ? a : gcd(b, a % b);   
    }
    
    static int sum(int [] arr){
        int sum = 0; 
        int i;
        for (i = 0; i < arr.length; i++)
            sum += arr[i];
        return sum;
    }
    
    static int lcm(int a, int b) {
		return (a / gcd(a, b)) * b;
	}
}

Earning A Profit CodeChef Solution in PYPY 3

class planet:
    def __init__(self,c,d):
        self.c=c
        self.d=d
for _ in range(int(input())):
    n,a=map(int, input().split())
    l=[]
    for i in range(n):
        c,d=map(int,input().split())
        l.append(planet(c,d))
    maxp=[[-1]*n for i in range(n)]
    minp=[[float('inf')]*n for i in range(n)]
    x=0
    prefix=[0]
    for i in range(n):
        prefix.append(prefix[-1]+l[i].c)
    for i in range(n):
        maxp[i][i]=l[i].d
        minp[i][i]=l[i].d
        j=i
        profit=a*(j-i+1)
        loss=prefix[j+1]-prefix[i]
        loss2=(maxp[i][j]-minp[i][j])**2
        x=max(x,profit-loss-loss2)
        for j in range(i+1,n):
            maxp[i][j]=max(maxp[i][j-1],l[j].d)
            minp[i][j]=min(minp[i][j-1],l[j].d)
            profit=a*(j-i+1)
            loss=prefix[j+1]-prefix[i]
            loss2=(maxp[i][j]-minp[i][j])**2
            x=max(x,profit-loss-loss2)
    print(x)

Earning A Profit CodeChef Solution in GO

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		N, A := readTwoNums(scanner)
		C := make([]int, N)
		D := make([]int, N)
		for i := 0; i < N; i++ {
			C[i], D[i] = readTwoNums(scanner)
		}
		fmt.Println(solve(N, A, C, D))
	}
}

func solve(N, A int, C []int, D []int) int64 {
	var best int64

	for i := 0; i < N; i++ {
		var sum int64
		max := 0
		min := 1 << 20
		for j := i; j < N; j++ {
			sum += int64(A - C[j])
			if D[j] > max {
				max = D[j]
			}
			if D[j] < min {
				min = D[j]
			}
			m := int64(max - min)
			tmp := sum - m*m
			if tmp > best {
				best = tmp
			}
		}
	}

	return best
}
Earning A Profit CodeChef Solution Review:

In our experience, we suggest you solve this Earning A Profit CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Earning A Profit CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Earning A Profit CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *