from typing import List
def find_first_occurrence(arr: List[int], target: int) -> int:
l, r = 0, len(arr) - 1
ans = -1
while l <= r:
mid = (l + r) // 2
if arr[mid] == target:
ans = mid
r = mid - 1
elif arr[mid] < target:
l = mid + 1
else:
r = mid - 1
return ans
if __name__ == '__main__':
arr = [int(x) for x in input().split()]
target = int(input())
res = find_first_occurrence(arr, target)
print(res)
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
class Solution {
public static int findFirstOccurrence(List<Integer> arr, int target) {
int targetIndex = -1;
int left = 0;
int right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr.get(mid) == target) {
targetIndex = mid;
right = mid - 1;
} else if (arr.get(mid) < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return targetIndex;
}
public static List<String> splitWords(String s) {
return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> arr = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
int target = Integer.parseInt(scanner.nextLine());
scanner.close();
int res = findFirstOccurrence(arr, target);
System.out.println(res);
}
}
function findFirstOccurrence(arr, target) {
let left = 0;
let right = arr.length - 1;
let targetIndex = -1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] == target) {
targetIndex = mid;
right = mid - 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return targetIndex;
}
function splitWords(s) {
return s == "" ? [] : s.split(' ');
}
function* main() {
const arr = splitWords(yield).map((v) => parseInt(v));
const target = parseInt(yield);
const res = findFirstOccurrence(arr, target);
console.log(res);
}
class EOFError extends Error {}
{
const gen = main();
const next = (line) => gen.next(line).done && process.exit();
let buf = '';
next();
process.stdin.setEncoding('utf8');
process.stdin.on('data', (data) => {
const lines = (buf + data).split('\n');
buf = lines.pop();
lines.forEach(next);
});
process.stdin.on('end', () => {
buf && next(buf);
gen.throw(new EOFError());
});
}
#include <algorithm> // copy
#include <iostream> // cin, cout, streamsize
#include <iterator> // back_inserter, istream_iterator
#include <limits> // numeric_limits
#include <sstream> // istringstream
#include <string> // getline, string
#include <vector> // vector
int find_first_occurrence(std::vector<int> arr, int target) {
int left = 0, right = arr.size() - 1, target_index = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
target_index = mid;
right = mid - 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return target_index;
}
template<typename T>
std::vector<T> get_words() {
std::string line;
std::getline(std::cin, line);
std::istringstream ss{line};
std::vector<T> v;
std::copy(std::istream_iterator<T>{ss}, std::istream_iterator<T>{}, std::back_inserter(v));
return v;
}
void ignore_line() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int main() {
std::vector<int> arr = get_words<int>();
int target;
std::cin >> target;
ignore_line();
int res = find_first_occurrence(arr, target);
std::cout << res << '\n';
}
import Data.Array (Array)
import qualified Data.Array.IArray as A
import Data.Array.IArray (bounds, listArray)
findFirstOccurrence :: Array Int Int -> Int -> Int
findFirstOccurrence arr target = uncurry (search (-1)) $ bounds arr where
search t l r
| l <= r = case compare (arr A.! m) target of
EQ -> search m l (pred m)
LT -> search t (succ m) r
GT -> search t l (pred m)
| otherwise = t
where m = l + (r - l) `div` 2
main :: IO ()
main = do
arr' <- map read . words <$> getLine
let arr = listArray (0, length arr' - 1) arr'
target <- readLn
let res = findFirstOccurrence arr target
print res