Pagini recente » Cod sursa (job #1930218) | Cod sursa (job #1206929) | Cod sursa (job #1284007) | Profil katonaaron | Cod sursa (job #2069614)
#include <stdio.h>
#define MAX_N 5000
int v[MAX_N];
const int L = 16;
typedef int(*BinSearchType)(int, int);
int BinSearch0(int n, int x)
{
int step = 1 << L;
int r = 0;
while(step != 0)
{
if(r + step <= n && v[r + step] <= x)
r += step;
step /= 2;
}
if(v[r] != x)
r = -1;
return r;
}
int BinSearch1(int n, int x)
{
int step = 1 << L;
int r = 0;
while(step != 0)
{
if(r + step <= n && v[r + step] <= x)
r += step;
step /= 2;
}
return r;
}
int BinSearch2(int n, int x)
{
int step = 1 << L;
int r = 0;
while(step != 0)
{
if(r + step <= n && !(v[r + step] >= x))
r += step;
step /= 2;
}
return r + 1;
}
BinSearchType binsearcharr[3] = {BinSearch0, BinSearch1, BinSearch2};
int main()
{
FILE *fin = fopen("cautbin.in", "r"),
*fout = fopen("cautbin.out", "w");
int n;
int t;
fscanf(fin, "%d", &n);
for (int i = 1; i <= n; i++)
fscanf(fin, "%d", &v[i]);
for (fscanf(fin, "%d", &t); t; t--)
{
int c, x;
fscanf(fin, "%d %d", &c, &x);
fprintf(fout, "%d\n", binsearcharr[c](n, x));
}
fclose(fin);
fclose(fout);
return 0;
}