Pagini recente » Cod sursa (job #2311569) | Cod sursa (job #292344) | Cod sursa (job #376945) | Cod sursa (job #1701974) | Cod sursa (job #2846163)
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 1024, BIG = 1048576;
int v[NMAX + 3];
struct ceva
{
int val, o1, o2;
} pos[BIG + 3];
bool cmp(ceva a, ceva b)
{
if (a.val != b.val)
return a.val < b.val;
}
inline bool check(ceva a, ceva b)
{
return !(a.o1 == b.o1 or a.o1 == b.o2 or a.o2 == b.o1 or a.o2 == b.o2);
}
int main()
{
ifstream cin("oite.in");
ofstream cout("oite.out");
int n, s, i, j;
cin >> n >> s;
for (i = 1; i <= n; i++)
cin >> v[i];
int dim = 0;
for (i = 1; i + 1 <= n; i++)
for (j = i + 1; j <= n; j++)
{
dim++;
pos[dim].val = v[i] + v[j];
pos[dim].o1 = i;
pos[dim].o2 = j;
}
sort(pos + 1, pos + dim + 1, cmp);
int ans = 0;
for (i = 1; i < dim; i++)
{
int need = s - pos[i].val;
if (need <= 0)
break;
int st, dr, med, sol1 = -1, sol2 = dim + 1;
st = i + 1, dr = dim;
while (st <= dr)
{
med = ((st + dr) >> 1);
if (pos[med].val >= need)
{
sol1 = med;
dr = med - 1;
}
else
st = med + 1;
}
if (sol1 == -1 or pos[sol1].val != need)
continue;
st = i + 1, dr = dim;
while (st <= dr)
{
med = ((st + dr) >> 1);
if (pos[med].val > need)
{
sol2 = med;
dr = med - 1;
}
else
st = med + 1;
}
for (j = sol1; j < sol2; j++)
if (check(pos[i], pos[j]))
ans++;
}
cout << ans / 3;
}