-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_two_points_nlogn.cpp
61 lines (54 loc) · 1.61 KB
/
AC_two_points_nlogn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: ac_two_points_nlogn.cpp
* Create Date: 2014-11-27 14:16:10
* Descripton: sort and find the answer with two points
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 0;
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
int sz = numbers.size();
int left = 0, right = sz - 1, sum = 0;
vector<int> sorted (numbers);
std::sort(sorted.begin(), sorted.end());
vector<int> index;
while (left < right) {
sum = sorted[left] + sorted[right];
if (sum == target) {
// find the answer
for (int i = 0; i < sz; i++) {
if (numbers[i] == sorted[left])
index.push_back(i + 1);
else if (numbers[i] == sorted[right])
index.push_back(i + 1);
if (index.size() == 2)
return index;
}
} else if (sum > target) {
right--;
} else {
left++;
}
}
// Program never go here, because
// "each input would have exactly one solution"
}
};
int main() {
Solution s;
vector<int> in;
int n, t;
cin >> n;
while (n--) {
cin >> t;
in.push_back(t);
}
cin >> t;
in = s.twoSum(in, t);
cout << in[0] << ' ' << in[1] << endl;
return 0;
}