[Mảng 1 Chiều Cơ Bản]. Bài 37. Tìm kiếm trong mảng 1 chiều

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Java 4.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng A[] gồm N phần tử, bạn hãy kiểm tra xem giá trị X có xuất hiện trong mảng không?

Gợi ý : Dùng mảng đánh dấu để mỗi test case chỉ mất O(1) thay vì tìm kiếm tuyến tính sẽ mất O(N)


Đầu vào

Dòng 1 là N : số phần tử trong mảng

Dòng 2 là N số trong mảng viết cách nhau 1 dấu cách

Dòng 3 là T : Số test case

T dòng tiếp theo mỗi dòng là số nguyên X


Giới hạn

1<=N<=10^6

0<=A[i]<=10^7

1<=T<=10^4

0<=X<=10^7


Đầu ra

Đối với mỗi test case in ra YES nếu X xuất hiện trong mảng, ngược lại in NO.


Ví dụ :

Input 01
9
2 41 21 28 27 3 49 22 2 
5
3
27
15
15
19
Output 01
YES
YES
NO
NO
NO

Comments

Please read the guidelines before commenting.



  • 0
    toanly2006  commented on Jan. 3, 2025, 5:10 p.m.

    include <iostream>

    include <climits>

    include <math.h>

    using namespace std; int mark[1000001] = { 0 }; int main() { int a[1005]; int n; cin >> n; for (int i = 0;i < n;i++) { cin >> a[i]; mark[a[i]] = 1; } int t; cin >> t; while (t--) { int x; cin >> x; bool found = false; if (mark[x] == 1) { found = true; } if (found == true) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }

    tại sao sai test case 4,5 vậy


  • 0
    thanhquan78  commented on Dec. 26, 2024, 8:58 a.m.

    Giúp em với e bị limit time ạ

    *#include<bits/stdc++.h> using namespace std;

    define rep(i,a,b) for(int i=a;i<b;i++)

    typedef long long ll;

    int main(){ ll n; cin>>n; ll a[n]; rep(i,0,n) cin>>a[i]; ll check; cin>>check; ll b[n]; rep(i,0,check) cin>>b[i];

    rep(i,0,check){
        bool ok = false;
        rep(j,0,n) if(b[i] == a[j]) {ok = true; break;}
        if(ok == true) cout<<"YES"<&lt;endl;
        else cout<<"NO"<&lt;endl;
    }
    

    }*


  • -6
    khuemih  commented on Aug. 26, 2024, 9:43 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -1
      doanbang042  commented on Sept. 14, 2024, 4:22 a.m.

      bạn cho mình hỏi code này của mình sao lại TLE ạ?


    • -2
      doanbang042  commented on Sept. 14, 2024, 4:22 a.m.

      include <stdio.h>

      include <math.h>

      int dem[1000001]; int ba(int a[],int n,int x){ int max=-1e9,cnt=0; for(int i=0;i<n;++i){ if(a[i]==x){ dem[a[i]]=1; if(a[i]>max){ max=a[i]; }

          }
      }
      for(int i=0;i&lt;n;++i){
          if(dem[a[i]]==1){
              cnt++;
              dem[a[i]]=0;
          }
      }
      if(cnt>0){
          return 1;
      }else{
          return 0;
      }
      

      } int main(){ int n; scanf("%d",&n); int a[n]; for(int i=0;i<n;++i){ scanf("%d",&a[i]); } int t; scanf("%d",&t); int x; for(int i=1;i<=t;++i){ scanf("%d",&x); if(ba(a,n,x)){ printf("YES\n"); }else{ printf("NO\n"); } } }


      • 0
        duongnc_pt  commented on Oct. 31, 2024, 12:54 p.m.

        Bạn có thể xem hoặc tìm hiểu thuật toán 'Mảng cộng dồn' nhé!

        Theo như code của bạn thì bị quá thời gian do do sử dụng tìm kiếm tuyến tính(Duyệt từ 0 -> n - 1) => độ phức tạp tốn O(n)

        Vì vậy mỗi testcase bạn phải duyệt ~10^6~ lần. Do vậy độ phức tạp của bạn là ~10^4~ * ~10^6~(Vượt qua mức an toàn là ~10^8~


  • -10
    namduongit  commented on July 20, 2024, 2:02 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.