1437C - Chef Monocarp - CodeForces Solution


dp flows graph matchings greedy math sortings *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <algorithm>
#define inf LONG_LONG_MAX

const int mod =  1000000007;
typedef long long int ll;
using namespace std;
void fastio()
{
    cin.tie(0);
    cin.sync_with_stdio(0);

}

int solve(int i,int j,vector<int>&arr,vector<vector<int>>&dp){
    if(i>=arr.size()){
        return 0;
    }
    if(j>=400)return 1e9;
    if(dp[i][j]!=-1)return dp[i][j];
    int ans=solve(i,j+1,arr,dp);
    ans=min(ans,abs(j-arr[i])+solve(i+1,j+1,arr,dp));
    return dp[i][j]=ans;


}
int main()
{

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
    int n;cin>>n;
    vector<int>arr(n);

    vector<vector<int>>dp(401,vector<int>(401,-1));
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    sort(arr.begin(),arr.end());
    cout<<solve(0,1,arr,dp)<<endl;
    }

    return 0;

}



Comments

Submit
0 Comments
More Questions

387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag
1056A - Determine Line
670B - Game of Robots
1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider
1486A - Shifting Stacks
1389B - Array Walk
71B - Progress Bar
701A - Cards