You are on page 1of 6

Bimarna pretraga

1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static int func(int[] a, int n, int s)
{
int zb = 0;
for (int i = 0; i < n; i++)
{
if (a[i] > s)
{
zb += a[i] - s;
}
}
return zb;
}
static int binarna_pretraga(int[] a, int[] arr, int l, int d, int max, int b, int
n)
{
int s;
if (l > d)
{
return max;
}
s = (d + l) / 2;
int bl = func(a, n, s);
if (bl >= b)
{
if (s > max)
{
max = s;
}
return binarna_pretraga(a, arr, s + 1, d, max, b, n);
}
else if (bl < b)
{
return binarna_pretraga(a, arr, l, s - 1, max, b, n);
}
else
{
return max;
}
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
int b = int.Parse(Console.ReadLine());
Array.Sort(a);
int d = a[n - 1];
int[] arr = new int[d];
for (int i = 0; i < d; i++)
{
arr[i] = i + 1;
}
int h = binarna_pretraga(a, arr, 0, d, 0, b, n);
Console.WriteLine(h);
Console.ReadLine();
}
}
}

2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static int funkcija( int b, string str1, string str2)
{
int br=0;
int k=0;
for(int i = 0; i < str2.Length;i++)
{
if (k < str1.Length - 1)
{
if (br == b)
{
k++;
br = 0;
}
}
else if (str2[i] == str1[k])
{
br++;
}
}
if (k == str1.Length - 1)
{
if (br >= b)
{
return br;
}
else
{
return -1;
}
}
else
{
return -1;
}
}
static int binarna_pretraga(int [] a, int l, int d, int max, string str1, string
str2)
{
int s;
if (l > d)
{
return max;
}
s = l + (d - l)/2;
int b = funkcija (s,str1,str2);
if (b == -1)
{
return binarna_pretraga(a, l, s - 1, max, str1, str2);
}
else if (b > max)
{
max = b;
return binarna_pretraga(a, s + 1, d, max, str1, str2);
}
else
{
return max;
}
}

static void Main(string[] args)


{
string str1 = Console.ReadLine();
string str2 = Console.ReadLine();
int br=0;
for (int i = 0; i < str2.Length; i++)
{
if (str2[i] == str1[0])
{
br++;
}
}
int [] a = new int [br];
for (int i = 0; i < br; i++)
{
a[i] = i + 1;
}
int n = binarna_pretraga(a, 0, br, 0,str1, str2);
Console.WriteLine(n);
Console.ReadLine();
}
}
}
3.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static int func(int[] arr, int n, int s)
{
int br = 0;
int max = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] >= s)
{
br++;
if (br >= max)
{
max = br;
}
}
else
{
br = 0;
}
}
return max;
}
static int binarna_pretraga(int[] a, int[] arr, int l, int d, int max, int n)
{
int s;
if (l > d)
{
return max;
}
s = (d + l) / 2;
int bl = func(a, n, s);
if (bl >= s)
{
if (s > max)
{
max = s;
}
return binarna_pretraga(a, arr, s + 1, d, max, n);
}
else if (bl < s)
{
return binarna_pretraga(a, arr, l, s - 1, max, n);
}
else
{
return max;
}
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] arr14 = new string[n];
arr14 = Console.ReadLine().Split();
int[] arr = new int[n];
int max = 0;
for (int i = 0; i < n; i++)
{
arr[i] = int.Parse(arr14[i]);
if (arr[i] > max)
{
max = arr[i];
}
}
if (max < n)
{
int[] abr = new int[max];
for (int i = 0; i < max; i++)
{
abr[i] = i + 1;
}
int h = binarna_pretraga(arr, abr, 0, max, 0, n);
Console.WriteLine(h*h);
}
else
{
int[] abr = new int[n];
for (int i = 0; i < n; i++)
{
abr[i] = i + 1;
}
int h = binarna_pretraga(arr, abr, 0, n, 0, n);
Console.WriteLine(h*h);
}
Console.ReadLine();
}
}
}

You might also like