Блог пользователя acash

Автор acash, история, 4 года назад, По-английски

how can we solve this problem Select team The language allowed was only C thats make problem more hard I tried to make all possible pairs store the pairs sum in sorted array ,then i started searching form end to check if it is possible that we can find two teams with maxsum But unable to handle like case 0 0 1 1

can anyone tell me how to approach this problem efficiently considering c language?

  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится -13 Проголосовать: не нравится

Please help

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Can't you just consider all ways to choose 1st, 2nd and 3rd student and check whether you have 4th one with skill equal to $$$(S_1+S_2-S_3)$$$? And then choose maximum among all possible teams.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      N=500 Will give tle don't you think O(n^4)

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        No, it's O(n^3*log(n)) because you choose 3 students and check 4th in O(log n) using unordered_map for example. Probably there is a way to choose in O(1), but I don't know how

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится +8 Проголосовать: не нравится

        It's not hard to do it in $$$O(n^3)$$$. Sort the array. Consider all $$$O(n^2)$$$ ways of choosing the first team. Now you have to find if there is a pair of elements, after erasing the first team from the array, with a given sum.

        The last problem can be solved in $$$O(n)$$$ time with a two pointers approach in a sorted array.