Cod sursa(job #2723966)

Utilizator ddiana.gamesDiana Games ddiana.games Data 15 martie 2021 23:14:47
Problema Transport Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 4.39 kb
import java.io.FileReader;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

public class Main {
//    public static int pow(int c) {
//        int p = -1;
//
//        while (c > 0) {
//            ++p;
//            c >>= 1;
//        }
//
//        return p;
//    }
//    public static int solution(int c) {
//        int a = 0, b = 0, d = pow(c), p;
//        while (d >= 0) {
//            p = 1 << d;
//            if (((c >> d) & 1) == 0) {
//                a += p;
//                b += p;
//            } else {
//                if (a > b) b += p;
//                else a += p;
//            }
//            --d;
//        }
//        return a * b;
//    }

//    public static int solution(int n, int k, int[] nums) {
//        int[][] digits = new int[10][10];
//        int no = 0;
//
//        for (int i = 0, digit = 0; i < n; ++i, digit = 0) {
//            while (nums[i] > 0) {
//                ++digits[digit][nums[i] % 10];
//                nums[i] /= 10;
//                ++digit;
//            }
//
//        }
//        for (int i = 9; i >= 0; --i, System.out.println())
//            for (int j = 0; j <= 9; ++j) {
//                if (digits[i][j] % k != 0)
//                    no = no * 10 + j;
//            }
//
//        return no;
//    }
//    public static int pow(int n, int p) {
//        if (p == 0) {
//            return 1;
//        } else {
//            int pp = pow(p / 2, n);
//            return (p & 1) == 0 ? pp * pp : n * pp * pp;
//        }
//    }
//
//    public static void solution(int[] nums) {
//        System.out.println((pow(2,3)));
//    }

//    public static int solution(int[] nums, int n) {
////        int s, step, pos, count = 0;
//        Arrays.sort(nums);
////        for (int i = 0; i < n - 1; ++i)
////            for (int j = i + 1; j < n; ++j) {
////                s = nums[i] + nums[j];
////                for (pos = j, step = 1 << 9; step > 0; step >>= 1)
////                    if (pos + step < n && s >= nums[step + pos]) {
////                        pos += step;
////                    }
////
////                count += pos > j ? pos + 1 - j : 0;
////            }
//        int s, pos = 2, count = 0;
//        if (n >= 3) {
//            for (int i = 0; i < n - 2 && pos <= n; ++i)
//                for (int j = i + 1; j < n - 1 && pos <= n; ++j, count += pos + 1 - j) {
//                    s = nums[i] + nums[j];
//                    while (pos + 1 < n && s >= nums[pos + 1]) ++pos;
//                }
//        }
//        System.out.println(count);
//        return count;
//    }

    public static int compute(int[] nums, int n, int c) {
        int count = 0, kk = 0;
        for (int i = 0; i < n; ++i)
            if (count + nums[i] <= c) {
                count += nums[i];
            } else {
                ++kk;
                count = nums[i];
            }
        return kk;
    }
    public static int solution(int[] nums, int n, int k) {
        int pos = 0, step;
//        for (int i = 0 ; i < n; ++i)
//            if (pos < nums[i])
//                pos = nums[i];
//        --pos;
        pos = 0;
        for (step = 1 << 20; step > 0; step >>= 1)
            if (compute(nums, n, pos + step) >= k)
                pos += step;
        System.out.println(pos + 1);
        return pos + 1;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner in = new Scanner(new FileReader("transport.in"));
        PrintStream out = new PrintStream("transport.out");

//        int t = in.nextInt(), c;
//        while (t-- > 0) {
//            out.print(solution(in.nextInt()));
//        }
//        int n = in.nextInt(), k = in.nextInt();
//        int[] nums = new int[n];
//        for (int i = 0; i < n; ++i)
//            nums[i] = in.nextInt();
//        out.print(solution(n, k, nums));

//        int[] nums = new int[10];
//        for (int i = 0; i < 10; nums[i] = in.nextInt(), ++i);
//        solution(nums);

//        int n = in.nextInt();
//        int[] nums = new int[n];
//        for (int i = 0; i < n; ++i)
//            nums[i] = in.nextInt();
//        solution(nums, n);

        // Trasport
        int n = in.nextInt(), k = in.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; ++i)
            nums[i] = in.nextInt();
        out.write(solution(nums, n, k));
        in.close();
        out.close();

    }
}