Sort by

recency

|

921 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/7L1lI6JScR8

    int luckBalance(int k, vector<vector<int>> contests) {
        sort(contests.begin(), contests.end(), [](vector<int> l, vector<int>r){
            return l[0] > r[0];
        });
        int ans = 0, im = 0;
        for(int i = 0; i < contests.size(); i++){
            if(im < k || contests[i][1] == 0) ans += contests[i][0];
            else ans -= contests[i][0];
            if(contests[i][1] == 1) im++;
        }
        return ans;
    }
    
  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM)
    import Data.List (partition, sortBy, splitAt)
    
    solve :: Int -> [[Int]] -> Int
    solve k trials = sum pluses - sum minuses + sum uvals
      where
        (important, unimportant) = partition (\(_ : t : _) -> t == 1) trials
        ivals = sortBy (flip compare) $ map head important
        uvals = map head unimportant
        (pluses, minuses) = splitAt k ivals
    
    main :: IO ()
    main = do
        [n, k] <- fmap (map read . words) getLine :: IO [Int]
        trials <- replicateM n $ fmap (map read . words) getLine :: IO [[Int]]
        print $ solve k trials
    
  • + 0 comments

    What the actual f? I understand the need to decorate the abstract CS problem with some story. But why not have the story take place in a reality that we all share and can relate to instead of authors' weired nightmares. Obfiscuating the meaning of the problem with bizare fantasies isn't making it more interesting or challenging in a productive way

  • + 0 comments

    def luckBalance(k, contests): important = [luck for luck, importance in contests if importance == 1] unimportant = [luck for luck, importance in contests if importance == 0]

    # Sort important contests in descending order of luck
    important.sort(reverse=True)
    
    # Sum luck from the k most important contests and subtract the rest
    return sum(important[:k]) - sum(important[k:]) + sum(unimportant)
    
  • + 0 comments
    #include<stdio.h>
     long int sort(int a[] ,int size,int k){
        int i,min,j,sum=0,t; 
        for(i=0;i<k;i++){
            min=i;
            for(j=i+1;j<size;j++){
                if(a[min]>a[j])
                min=j;
            }
            if(i!=min){
                t=a[i];
                a[i]=a[min];
                a[min]=t;
            }
        sum+=a[i];    
        }
        return sum;    
    }
    int main(){
        int n,k,i,t;
        long int l,sum1=0,sum2;
        int ar[100],arc=0;
        scanf("%d %d",&n,&k);
        for(i=0;i<n;i++){
            scanf("%ld %d",&l,&t);
            if(t==1)
            ar[arc++]=l;
            sum1+=l;
        }
        sum2=sort(ar,arc,arc-k);
        printf("%ld",(sum1- 2*(sum2)));
        return 0;
    }