Pagini recente » Cod sursa (job #718207) | Cod sursa (job #1335327) | Cod sursa (job #2395717) | Cod sursa (job #110704) | Cod sursa (job #2229684)
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdint>
#include <cstddef>
#include <string>
#include <array>
#include <vector>
#include <list>
#include <algorithm>
#include <utility>
#include <type_traits>
#include <limits>
#include <cmath>
std::ifstream f{ "cautbin.in" };
std::ofstream g{ "cautbin.out" };
inline namespace globals {
constexpr std::size_t NMAX = 100000;
std::array<int, NMAX> numbers;
std::size_t number_of_queries;
std::size_t array_size;
}
enum QueryType : int
{
BIGGEST_POS = 0,
BIGGEST_SMALLER,
SMALLEST
};
int binary_search_0(const int t_key,
int t_left = 0, int t_right = array_size - 1)
{
int mid{ 0 };
while(t_left < t_right) {
mid = t_left + (t_right - t_left) / 2;
if(t_key <= numbers[mid]) {
t_left = mid + 1;
}
else {
t_right = mid;
}
}
mid = t_left + (t_right - t_left) / 2;
if(numbers[mid] > t_key) {
--mid;
}
if(numbers[mid] != t_key) {
return -1;
}
return mid;
}
int binary_search_1(const int t_key,
int t_left = 0, int t_right = array_size - 1)
{
int mid{ 0 };
while(t_left < t_right) {
mid = t_left + (t_right - t_left) / 2;
if(t_key < numbers[mid]) {
t_right = mid;
}
else {
t_left = mid + 1;
}
}
mid = t_left + (t_right - t_left) / 2;
if(numbers[mid] > t_key) {
--mid;
}
return mid;
}
int binary_search_2(const int t_key,
int t_left = 0, int t_right = array_size - 1)
{
int mid{ 0 };
while(t_left < t_right) {
mid = t_left + (t_right - t_left) / 2;
if(numbers[mid] < t_key) {
t_left = mid + 1;
}
else {
t_right = mid;
}
}
mid = t_left + (t_right - t_left) / 2;
if(numbers[mid] < t_key) {
++mid;
}
return mid;
}
void read()
{
f >> array_size;
std::size_t index;
for(index = 0; index < array_size; ++index) {
f >> numbers[index];
}
f >> number_of_queries;
for(index = 0; index < number_of_queries; ++index) {
int type{ -1 };
int key{ 0 };
f >> type >> key;
switch(type)
{
case BIGGEST_POS:
{
int x = binary_search_0(key);
g << (x == -1 ? -1 : x + 1) << '\n';
break;
}
case BIGGEST_SMALLER:
{
g << binary_search_1(key) + 1 << '\n';
break;
}
case SMALLEST:
{
g << binary_search_2(key) + 1 << '\n';
break;
}
default:
{
break;
}
}
}
}
/*
void solve()
{
}
*/
int main(const int, const char *[])
{
read();
// solve();
return 0;
}