fork(1) download
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <functional>
  5.  
  6. std::vector<int> solution (std::vector<int>& prices, int cost)
  7. {
  8. std::vector<int> result;
  9. std::vector<int> sorted = prices;
  10.  
  11. std::sort(sorted.begin(), sorted.end());
  12.  
  13. for (int i = 0; i < prices.size(); i++)
  14. {
  15. if (std::binary_search(sorted.begin(), sorted.end(), cost - prices[i]))
  16. {
  17. result.push_back (i); // +1 because we count from 1
  18. // using linear search
  19. for (int j = i + 1; j < prices.size(); j++)
  20. if (prices[i] + prices[j] == cost)
  21. {
  22. result.push_back (j); // +1 because we count from 1
  23. return result;
  24. }
  25. }
  26. }
  27. return result;
  28. }
  29.  
  30. int main() {
  31. int ntests, cost, num_of_flavors, curr_price;
  32. std::vector<int> prices, answer;
  33.  
  34. std::cin >> ntests;
  35. for (int i = 0; i < ntests; i++)
  36. {
  37. std::cin >> cost >> num_of_flavors;
  38.  
  39. // Input prices
  40. for (int j = 0; j < num_of_flavors; j++)
  41. {
  42. std::cin >> curr_price;
  43. prices.push_back (curr_price);
  44. }
  45. answer = solution (prices, cost);
  46. std::cout << (answer[0] + 1) << " " << (answer[1] + 1) << std::endl;
  47. prices.clear();
  48. }
  49. return 0;
  50. }
Success #stdin #stdout 0s 3464KB
stdin
2
4
5
1 4 5 3 2
4
4
2 2 4 3
stdout
1 4
1 2