10 条题解

  • 7
    @ 2023-1-4 11:38:24
    #include <iostream>
    using namespace std;
    int a[100005];
    int main()
    {
        int n, q, x;
        cin >> n;
        for (int i = 1 ; i <= n ; i++)
            cin >> a[i];
        cin >> q;
        for (int i = 1 ; i <= q ; i++)
        {
            cin >> x;
            int l = 1, r = n, mid;
            while (l <= r)
            {
                mid = (l + r) / 2;
                if (a[mid] < x)
                    l = mid + 1;
                else if (a[mid] > x)
                    r = mid - 1;
                else
                    break;
            }
            int first = mid;
            while (a[first] == x)
                first--;
            if (first == mid) 
                first = -1;
            else 
                first++;
            cout << first << " ";
        }
        return 0;
    } //代码已AC
    
    • 2
      @ 2024-5-12 17:16:19
      #include<bits/stdc++.h>
      using namespace std;
      int main(){
      int a[100000],b,c,d,e;cin>>b;
      for(e=1;e<=b;e++)cin>>a[e];
      cin>>c;
      for(e=1;e<=c;e++){cin>>d;if(a[lower_bound(a+1,a+b+1,d)-a]==d){cout<<lower_bound(a+1,a+b+1,d)-a<<' ';}else cout<<-1<<' ';}
      return 0;
      }//代码已AC
      
      • 2
        @ 2023-9-29 13:21:58

        服了呀!8个AC,2个TLE!

        #include <iostream>
        using namespace std;
        int main()
        {
            int n,q,x;
            cin>>n;
            int a[n+1];
            for (int i=1;i<=n;i++)cin>>a[i];
            cin>>q;
            for (int i=1;i<=q;i++)
            {
                cin>>x;
                int ans=0;
                for (int j=1;j<=n;j++)
                {
                    if (a[j]==x)
                    {
                        ans++;
                        cout<<j<<" ";
                        break;
                    }
                }
                if (ans==0)cout<<"-1 ";
            }
            return 0;
        }
        
        • @ 2024-5-12 17:17:36

          改成这样就可以了:

          #include<bits/stdc++.h>
          using namespace std;
          int main(){
          int a[100000],b,c,d,e;cin>>b;
          for(e=1;e<=b;e++)cin>>a[e];
          cin>>c;
          for(e=1;e<=c;e++){cin>>d;if(a[lower_bound(a+1,a+b+1,d)-a]==d){cout<<lower_bound(a+1,a+b+1,d)-a<<' ';}else cout<<-1<<' ';}
          return 0;
          }
          
      • 2
        @ 2023-7-1 14:06:34
        #include <iostream>
        #include <algorithm>
        using namespace std;
        int a[100001],n,q,x;
        int main()
        {
            cin>>n;
            for (int i=1;i<=n;i++)
                cin>>a[i];
            cin>>q;
            for (int i=1;i<=q;i++)
            {
                cin>>x;
                if (a[lower_bound(a+1,a+n+1,x)-a]==x)
                    cout<<lower_bound(a+1,a+n+1,x)-a<<' ';
                else
                    cout<<-1<<' ';
            }
        }
        
        • 2
          @ 2023-6-25 19:33:35
          #include <bits/stdc++.h>
          using namespace std;
          int a[10000005];
          int main()
          {
              int n, m, x, l, r, ans;
              cin >> n;
              for (int i = 1; i <= n; i++)
                  cin >> a[i];
              cin >> m;
              for (int i = 1; i <= m; i++){
                  cin >> x;
                  l = 1;
                  r = n;
                  ans = -1;
                  while (l <= r){
                      int mid = (l + r) / 2;
                      if (a[mid] >= x){
                          if (a[mid] == x)
                              ans = mid;
                          r = mid - 1;
                      }else{
                          l = mid + 1;
                      }
                  }
                  cout << ans << " ";
              }
          }
          
          • 1
            @ 2022-8-21 15:36:40
            for (int i=1;i<=q;i++)
            {
                cin>>x;
                ans=-1;
                l=1;
                r=n;
                while (l<=r)
                {
                    int mid=(l+r)/2;
                    if (a[mid]>=x)
                    {
                        if (a[mid]==x)//这里由于大于等于都算了进去,无法确定是否为x,需再判断一次
                        {
                            ans=mid;
                        }
                        r=mid-1;
                    }
                    else
                    {
                        l=mid+1;
                    }
                }
                cout<<ans<<" ";
            }
            
            • 0
              @ 2023-8-3 14:32:47
              #include<bits/stdc++.h>
              using namespace std;
              int a[1000001],n,x;
              int Find(int low,int high)
              {
              	if(a[low]>x||a[high]<x||low>high)
              		return -1;
              	
              	int mid=(low+high)/2;
              	if(a[mid]==x)
              	{
              		if(low==high) //虽然找到 a[mid]=x,但是它不一定是首次出现的,还要继续找 
              			return mid;
              		else
              			return Find(low,mid);
              	}
              	else if(a[mid]<x)
              		return Find(mid+1,high);
              	else
              		return Find(low,mid-1);
              }
              int main()
              {
              	int i,q;
              	scanf("%d",&n);
              	for(i=1;i<=n;i++)
              		scanf("%d",&a[i]);
              	
              	scanf("%d",&q);
              	
              	while(q--)
              	{
              		scanf("%d",&x);	
              		printf("%d ",Find(1,n));
              	}
              	
              
              	return 0;
              }
              
              • 0
                @ 2023-7-20 17:50:55
                #include <cstdio>
                using namespace std;
                int main(){
                    int n,x,q,l,r,mid,pos,a[100001];
                    scanf("%d",&n);
                    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
                    scanf("%d",&q);
                    for(int i=1;i<=q;i++){
                        l=1,r=n,pos=-1;
                        scanf("%d",&x);
                        while(l<=r){
                            mid=(l+r)/2;
                            if(a[mid]==x)pos=mid;
                            a[mid]>=x?r=mid-1:l=mid+1;
                        }
                        printf("%d%c",pos,' ');
                    }
                    printf("\n");
                    return 0;
                }
                
                • 0
                  @ 2023-6-27 10:09:32
                  #include <bits/stdc++.h>
                  using namespace std;
                  int n,q,x;
                  int a[100005];
                  int main(){
                      cin>>n;
                      for (int i=1;i<=n;i++){
                          cin>>a[i];
                      }
                      sort(a+1,a+n+1);
                      cin>>q;
                      while (q--){
                          cin>>x;
                          int l=1,r=n;
                          while (l<=r){
                              int mid=(l+r)>>1;
                              if (a[mid]>=x){//大于等于可以保证找到第一次出现的位置
                                  r=mid-1;
                              }else{
                                  l=mid+1;
                              }
                          }
                          cout<<(a[l]==x?l:-1)<<" ";//特判找不到的情况
                      }
                      return 0;
                  }
                  
                  • -2
                    @ 2022-4-24 16:09:10

                    鼓励大家写题解,但注意题解格式。

                    给代码两端加上这个会舒服一些

                    ```cpp

                    你的代码

                    ```

                    </span>

                    这个点在键盘的左上角tab上面那个键,注意切换输入法

                    #include<iostream>
                    using namespace std;
                    int main()
                    {
                        int n;
                        cin>>n;//这是一个注释
                        return 0;
                    } 
                    

                    请注意严禁抄袭题解,写题解不要只放代码,需加上你的思路或代码注释。

                    抄袭题解一经发现直接取消成绩。

                    • 1

                    信息

                    ID
                    890
                    时间
                    1000ms
                    内存
                    128MiB
                    难度
                    6
                    标签
                    递交数
                    1168
                    已通过
                    390
                    上传者