█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 6771

Problema 0x63 - Encontrar el primer elemento mayor a X en un arreglo ordenado

  • Autor: danidiaztech
  • Fecha: 2022-11-15 02:19:26 UTC (Hace más de 1 año)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.006 s 1 KBi
#2
Incorrecto
0.007 s 1 KBi
#3
Incorrecto
0.004 s 1 KBi
#4
Incorrecto
0.005 s 1 KBi
#5
Incorrecto
0.005 s 1 KBi
#6
Incorrecto
0.004 s 1 KBi
#7
Incorrecto
0.004 s 1 KBi
#8
Incorrecto
0.005 s 1 KBi
#9
Incorrecto
0.005 s 1 KBi
#10
Incorrecto
0.004 s 1 KBi
#11
Incorrecto
0.004 s 1 KBi
#12
Incorrecto
0.017 s 2 KBi
#13
Incorrecto
0.015 s 2 KBi
#14
Correcto
0.033 s 2 KBi
#15
Correcto
0.02 s 2 KBi
#16
Correcto
0.024 s 2 KBi
#17
Correcto
0.026 s 2 KBi
Puntos totales: 30 / 100

Código

// Made by Daniel Diaz (@Danidiaztech)
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
#define forn(i, n) for (int i = 0; i < n; i++) // for in range in python
#define FOR(i, a, b) for (int i = a; i < b; i++) // for in range in python
#define int long long int
#define double long double
#define pb push_back
#define ff first
#define ss second
#define mk make_pair

typedef pair<int, int> pii;

const int MAX = 1e6;
const int MIN = -MAX;
const int INF = LLONG_MAX;
const int MINF = LLONG_MIN;
const int MOD = 1e9 + 7;

// int arr[MAX];
void solve_upper_bound(){
  int n;
  cin >> n;
  int a[n];
  forn(i,n) cin >> a[i];
  int q;
  cin >> q;
  while (q--){
    int x; cin >> x;
    auto it = upper_bound(a, a + n, x);
    cout << (int)(it - a) << endl;
  }
}

void solve(){
  int n;
  cin >> n;
  int a[n];
  forn(i,n) cin >> a[i];
  int q;
  cin >> q;
  while (q--){
    int x; cin >> x;
    if (x > a[n - 1]){
      cout << n << endl;
      continue;
    }
    int l = 0, r = n - 1;
    // At some point, l will overlap r
    while (l != r){
      int mid = (l + r) >> 1;
      if (a[mid] > x){
        // This is the last element that could be the valid answer
        r = mid; 
      }
      else l = mid + 1;
    }
    cout << l << endl;
  }
}

int32_t main() {
  fastInp;
  #if LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif

  int tc = 1;
  // cin >> tc;

  for (int t = 1; t <= tc; t++){
    // cout << "Case #" << t << ": ";
    solve();
  }
  return 0;
}