Showing posts with label Greedy. Show all posts
Showing posts with label Greedy. Show all posts

Friday, 5 February 2021

Sum of Factorials Lightoj 1189

 In this problem we will pre calculate all the factorial values upto 20(because the constraint). Then greedily we will just check can we take the value of factorial of any number.

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
int main()
{
    IOS
    int t;
    cin>>t;
    ll fact[21];
    fact[0] = 1;
    for(int i=1;i<=20;i++)
    {
        fact[i] = fact[i-1]*i;
        //cout << fact[i] << endl;
    }
    for(int tc=1;tc<=t;tc++)
    {
        ll n;
        cin>>n;
        vector<int>vec;
        for(int i=20;i>=0;i--)
        {
            if(fact[i]<=n)
            {
                n-=fact[i];
                vec.pb(i);
            }
            if(n==0) break;
        }
        sort(vec.begin(),vec.end());
        cout << "Case "<< tc << ": ";
        if(n!=0) {cout <<"impossible\n";continue;}
        for(int i=0;i<vec.size();i++)
        {
            if(i>0 && i<vec.size())
                cout << "+";
            cout << vec[i] << "!";
        }
        cout << endl;
    }
    return 0;
}

Conquering Keokradong Lightoj 1048

 we will check which value is most satisfied in the given number of nights k. For this purpose we will do binary search from highest distance of two campsites to sum of distance.

/// Bismillahir Rahmanir Rahim
/* Mohammad Morsalin
   Dept of ICE, NSTU
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define mp make_pair
//#define endl "\n"
#define int long long
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ms2d(x,m,n) memset(x, 0, sizeof(x[0][0]) * m * n)
#define uniq(vec) vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end())))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define pi pair<int,int>
#define tc(t) int t;scanf("%lld",&t);while(t--)
#define bits(n) __builtin_popcount(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int> >
#define ins insert
#define ALL(v) v.begin(),v.end()
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
#define Inf INFINITY
#define minv(v) *min_element(v.begin(),v.end())
#define maxv(v) *max_element(v.begin(),v.end())
#define fi first
#define se second
#define PI acos(-1)
#define sz(a) (int)a.size();
#define IOS ios::sync_with_stdio(false);
using namespace std;
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const long long MOD = 1000000007;
double sq(double x) {return x*x;}
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
signed main()
{
    IOS
    int tn=0;
    tc(t){
        int n,k;
        scanf("%lld%lld",&n,&k);
        int arr[n+1];
        int sum=0,max_val=0;
        f0(n+1){
            scanf("%lld",&arr[i]);
            sum+=arr[i];
            max_val=max(max_val,arr[i]);
            //cout << arr[i]<< endl;
        }
        int low=max_val;
        int high=sum;
        int mid;
        int ans=high;
        while(low<=high){
            mid=(low+high)/2;
            int time=0,pre=0;
           // cout << mid;
            for(int i=0;i<n+1;i++){
                pre+=arr[i];
                if(pre>mid){
                    time++;
                    pre=arr[i];
                }
            }
           // cout << " "<< time << endl;
            if(time<=k){
                high=mid-1;
                ans=mid;
            }
            else{
                low=mid+1;
            }
        }
        printf("Case %lld: %lld\n",++tn,ans);
        int pre=0;
        int nt=0;
        for(int i=0;i<n+1;i++){
            if(pre+arr[i]>ans){
                printf("%lld\n",pre);
                pre=0;
                i--;
                nt++;
            }
            else if(pre+arr[i]==ans){
                printf("%lld\n",pre+arr[i]);
                pre=0;
                nt++;
            }
            else if(k-nt==(n-i)){
                printf("%lld\n",pre+arr[i]);
                pre=0;
                nt++;
            }
            else pre+=arr[i];
        }
    }
    return 0;
}
///Alhamdulillah


Sunday, 14 April 2019

Bishops LightOj 1202

In this problem you just have to find out some conditions relevant to row and column differences.
One important thing is maximum move will be two(where one bishop can reach another but they are not in same diagonal)

so, you have to just find out conditions for each output(1,2 and impossible)


code:
/*
Author : Mohammad Morsalin Hossain
Dept of ICE,NSTU
*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
int main()
{
    IOS
    int test;
    cin>>test;
    int to=0;
    while(test--)
    {
        int row1,col1,row2,col2;
        cin>>row1>>col1>>row2>>col2;
        if((row1+col2 == row2+col1)||(abs(row1-row2)==abs(col1-col2)))
            cout << "Case "<<++to<<": 1\n";
        else if((row1+col2)==abs(row2-col1)||(abs(row1-col2)==row2+col1)||(row1+row2)%2==(col1+col2)%2)
            cout << "Case "<<++to<<": 2\n";
        else
            cout <<"Case "<<++to<<": impossible\n";
    }
    return 0;
}

Monkey Banana Problem lightoj 1004

  In this problem we will check which adjacent cell will benefits the monkey best. For this, we will check all possible solution of this pro...