Skip to content

Instantly share code, notes, and snippets.

@euyuil
Created February 21, 2014 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euyuil/9128145 to your computer and use it in GitHub Desktop.
Save euyuil/9128145 to your computer and use it in GitHub Desktop.
Codeforces 395A: Skis. 2-Sum problem. 1 solution for both 395A1 and 395A2. (http://codeforces.com/problemset/problem/395/A1, http://codeforces.com/problemset/problem/395/A2)
// 395A1 & 395A2 - Skis
// http://codeforces.com/problemset/problem/395/A1
// http://codeforces.com/problemset/problem/395/A2
#include <cstdlib>
#include <iostream>
#include <algorithm>
const int N = 1111111;
int n, q, arr[N];
using namespace std;
int main()
{
cin >> n >> q;
for (int i = 0; i < n; ++i)
cin >> arr[i];
sort(arr, arr + n);
int ans = 0;
{
int i = 0, j = n - 1;
while (i < j)
{
if (arr[i] + arr[j] > q)
--j;
else if (arr[i] + arr[j] < q)
++i;
else
--j, ++i, ++ans;
}
}
cout << ans << endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment