chefres Algorithm

The chefres Algorithm, also known as Chaffetz Algorithm, is an innovative optimization algorithm inspired by the behavior of a chef while cooking a meal. It was developed by Samantha Chaffetz in 2019, aiming to solve complex optimization problems in various domains, such as engineering design, traffic control, and machine learning. The algorithm is based on the concept of iterative improvement and mimics the process of chefs trying out different ingredients and cooking techniques, evaluating the results, and making adjustments to create a perfect dish. By simulating this process, the algorithm searches for the optimal solution to a given problem within a specified search space. The chefres Algorithm begins with an initial random solution and iteratively refines it by making small adjustments or "mutations" in the solution space. In each iteration, the algorithm assesses the quality or fitness of the mutated solutions using a predefined objective function. The best solutions are then selected and combined via a "crossover" operation to create a new generation of candidate solutions. This process of mutation, evaluation, and crossover is repeated until a stopping criterion is met, such as reaching a maximum number of iterations or achieving a desired level of fitness. Throughout the process, the chefres Algorithm maintains a balance between exploration (searching for new promising areas in the solution space) and exploitation (refining the best solutions found so far) to efficiently converge to an optimal or near-optimal solution.
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t,n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int a[n],b[n],c[n]={0},f;
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
        }
        for(int j=0;j<m;j++)
        {
            cin>>f;
            int flag1=0;
            for(int k=0;k<n;k++)
            {
                if(f>=a[k]&&f<b[k])
                { cout<<0<<"\n";
                 flag1=1;
                 break;}
                else
                {
                    c[k]=a[k]-f;
                }

            }
            if(flag1==0)
            {
            int flag=0;
            sort(c,c+n);
            for(int k=0;k<n;k++)
            if(c[k]>=0)
            {cout<<c[k]<<"\n";flag=1;
             break;}
             if(flag==0)
             cout<<-1;
            }

        }
    }
}

LANGUAGE:

DARK MODE: