10 solutions

  • 4
    @ 2024-2-23 19:38:18

    题意

    A+BA+B 等于 mm 的那两个数(A<BA<B)。如果有多个 AABB,那就输出 AA 较小的那一对数

    思路

    先定下AA,然后找 BB 在不在数组里

    PS:找 BB 用二分,找之前要先排序

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int a[100005],n,m;
    bool f(int l,int r,int p){
    	while (l <= r){
    		int mid = (l + r) >> 1;
    		if (p == a[mid])	return 1;	// 找到了
    		else if (p < a[mid])	r = mid - 1;	// 在左边
    		else	l = mid + 1;	// 在右边
    	}
    	return 0;	// 没找到
    }
    int main(int argc, char **argv){
    	cin >> n;
    	for (int i = 0;i < n;i++)	cin >> a[i];
    	cin >> m;	// 标准
    	stable_sort(a,a + n);	// 先排序
    
    	int l = 0,r = n - 1;	// 左边界右边界
    	int ms,md;	// ms 是小的那个数,md 是大的那个数
    	for (int i = 0;i < n - 1;i++){
    		ms = a[i];
    		md = m - ms;
    		if (f(i + 1,n - 1,md)){	// 找那个大的数在不在数组里(f 可以换成 binary_search)
    			printf("%d %d",ms,md);	// 有就输出
    			return 0;	// 找到就结束了
    		}
    	}
    	printf("No");
    	return 0;
    }
    

    update2024/2/23update 2024/2/23:修复了“样例过不了”的 bug

    • 4
      @ 2024-1-26 9:30:33
      #include<bits/stdc++.h>
      
      using namespace std;
      
      typedef int Steve;
      typedef long long Alex;
      
      const Steve maxn = 1e5 + 5;
      Steve a[maxn];
      
      Steve minecraft(Steve l, Steve r, Steve p){
      	Steve L = l;
      	Steve R = r;
      	while(L <= R){
      		Steve mid = L + (R - L) / 2;
      		if(p == a[mid]){
      			return mid;
      			
      		}
      		else if(p > a[mid]){
      			L = mid + 1;
      			
      		}
      		else{
      			R = mid - 1;
      			
      		}
      	}
      	return -1;
      	
      }
      
      int main(){
      	Steve n, m;
      	cin >> n;
      	for(int i = 0; i < n; i++){
      		cin >> a[i];
      	}
      	cin >> m;
      	sort(a, a + n);
      	for(Steve i = 0; i < n - 1; i++){
      		Steve res = m - a[i];
      		if(minecraft(i + 1, n - 1, res) != -1){
      			cout << a[i] << " "<< res;
      			return 0;
      		}
      	}
      	cout << "No";
      	return 0;
      }
      
    • 2
      @ 2024-2-23 20:26:06

      《可以AC但过不了样例的歹马》

      #include<iostream>
      #include<algorithm>
      using namespace std;
      int n,m,a[100001];
      int main()
      {
      	cin >> n;
      	for (int i=0;i<n;i++)
      	{
      		cin >> a[i];
      	}
      	cin >> m;
      	sort(a,a+n);
      //	for (int i=0;i<n;i++)
      //	{
      //		cout << a[i];
      //	}
      	for (int i=0;i<n-1;i++){
      		int q=m-a[i];
      		int l=i+1,r=n-1;
      		int mid;
      		while(l<r){
      			mid=(l+r)>>1;
      			//cout<< l << " " << r << " "<<mid<<"\n";
      			if (q==a[mid]){
      				cout<< a[i] << " " << q;
      				return 0;
      			}
      			if (a[mid]>q){
      				r=mid-1;
      			}else{
      				l=mid+1;
      			}
      			//cout<< l << " " << r << " "<<mid<<"\n";
      		}		
      	}
      	cout << "No";
      	return 0;
       }
      

      《真正AC的代码》

      #include<iostream>
      #include<algorithm>
      using namespace std;
      int n,m,a[100001];
      int main()
      {
      	cin >> n;
      	for (int i=0;i<n;i++)
      	{
      		cin >> a[i];
      	}
      	cin >> m;
      	sort(a,a+n);
      //	for (int i=0;i<n;i++)
      //	{
      //		cout << a[i];
      //	}
      	for (int i=0;i<n-1;i++){
      		int q=m-a[i];
      		int l=i+1,r=n-1;
      		int mid;
      		while(l<=r){
      			mid=(l+r)>>1;
      			//cout<< l << " " << r << " "<<mid<<"\n";
      			if (q==a[mid]){
      				cout<< a[i] << " " << q;
      				return 0;
      			}
      			if (a[mid]>q){
      				r=mid-1;
      			}else{
      				l=mid+1;
      			}
      			//cout<< l << " " << r << " "<<mid<<"\n";
      		}		
      	}
      	cout << "No";
      	return 0;
       }
      
      • 2
        @ 2024-2-23 20:12:39
        #include<iostream>
        #include<algorithm> //加sort和binary search
        int n,m,num[100001];
        int main(){
            cin>>n;
            for(int i=0;i<n;i++) cin>>num[i];
            cin>>m; //cin
            sort(num,num+n);//排序
            for(int i=0;i<n;i++){
                if(binary_search(num+1,num+n,m-num[i])==1){ //查找某个数
                    cout<<num[i]<<" "<<m-num[i];
                    return 0; //找到直接cout然后return 0
                }
            }
            cout<<"No";
        }//有防抄袭
        
        • 1
          @ 2024-7-16 18:02:41

          更简单的题解,思路可以看其他大佬发的

          #include<bits/stdc++.h>
          /*十分简单题解,省下非常多行*/ 
          using namespace std;
          #define int long long/*防超int(虽然2^30应该不超)*/ 
          int n,m,a[1640000];//要超过10^6 
          signed main(){
          	cin>>n;
          	for(int i=0;i<n;i++){
          		cin>>a[i];
          	}
          	cin>>m;//输入 
          	sort(a,a+n);//保证使用lower_bound不出错,同时省时间 
          	for(int i=0;i<n;i++){
          		int k=lower_bound(a,a+n,m-a[i])-a;//lower_bound返回第一个大于等于给定值的地址 
          		if(k!=n){
          			if(a[i]+a[k]==m){
          				cout<<a[i]<<' '	<<a[k]<<endl;
          				return /*164*/0;//若满足,直接return 
          			}
          		}
          	}
          	cout<<"No"<<endl;//否则输出no 
          	return /*164*/0;
          }
          
          • 1
            @ 2024-2-23 19:22:18
            #include<bits/stdc++.h>
            using namespace std;
            long long a[100001];
            bool cmp(int x,int y){
            	return x<y;
            }
            int f();
            int main(){
                int n;
                cin>>n;
                for(int i=0;i<n;i++){
                	cin>>a[i];
            	}
                long long k;
                cin>>k;
                sort(a,a+n,cmp);
            	for(int i=0;i<n;i++){
            		if(binary_search(a,a+n,k-a[i])==1){
            		   cout<<a[i]<<" "<<k-a[i];
            		   return 0;
            		}
            	}
            	cout<<"No";
            }
            
            • 0
              @ 2025-3-10 19:29:45

              C24的题解集合👏欢迎补充

              //双指针(尺取法),O(n)。但是本题给的乱序序列所以加上排序时间实际上O(nlogn) 
              //@C24zhongzifan 
              #include<bits/stdc++.h>
              using namespace std;
              int n,a[1000005],s;
              int main(){
              	cin>>n;
              	for(int i=1;i<=n;i++){
              		cin>>a[i];
              	}
              	cin>>s;
              	sort(a+1,a+n+1);
              	int l=1,r=n;
              	while(l <= r){
              		if(a[l]+a[r] == s){
              			cout<<a[l]<<' '<<a[r];
              			return 0;
              		} 
              		if(a[l]+a[r] > s){
              			r--;
              		} 
              		if(a[l]+a[r] < s){
              			l++;
              		} 
              	}
              	cout<<"No";
              	return 0;
              } 
              /*
              //枚举+手写二分查找O(nlogn)
              //@传奇猴哥 (C24wanghongming)
              #include <iostream> 
              using namespace std;
              int a[100010],t,n,l,r,m;
              bool f;
              int main(){
              	cin>>n;
              	for(int i=0;i<n;i++)cin>>a[i];
              	cin>>t;
              	sort(a,a+n);
              	for(int i=0;i+1<n;i++){
              		int c=t-a[i];
              		l=i,r=n,f=false;
              		while(l+1!=r){
              			m=(l+r)/2;
              			if(a[m]<c)
              				l=m;
              			else 
              				r=m;
              			if(a[m]==c)
              				f=true;
              		}
              		if(f){
              			cout<<a[i]<<" "<<a[r];
              			return 0;
              		}
              	}
              	cout<<"No";
              	return 0;
              }
              //map(红黑树本质二分)
              //@C24zhouyanchen 
              #include <iostream>
              #include <map>
              using namespace std;
              map <int,int> a;
              int n,target,tmp;
              int main(){
              	cin >> n;
              	for (int i = 1;i <= n;i++){
              		cin >> tmp;
              		a[tmp]++;
              	}
              	cin >> target;
              	map <int,int> :: iterator it = a.begin();
              	for (;it != a.end();it++){
              		if (it->first <= target - it->first && a[target - it->first] > 0){
              			cout << it->first << ' ' << target - it->first << '\n';
              			return 0;
              		}
              	}
              	cout << "No\n";
              	return 0;
              } 
              
              //multiset(set本质固定value的map)
              //@nPr123f (C24zhengfujia)
              //对比以下2组用例3的个数即知为何不可用set 
              //input1:
              //6
              //1 3 3 1 1 8
              //6 
              //input2:
              //6
              //1 1 3 1 1 8
              //6
              
              #include<cstdio>
              #include<set>
              using namespace std;
              multiset<int> s;
              int n,m,t;
              int main()
              {
              	scanf("%d",&n);
              	for(int i=0;i<n;++i){
              		scanf("%d",&t);
              		s.insert(t);
              	}
              	scanf("%d",&m);
              	for(auto it=s.begin();it!=s.end();++it)
              		if(s.count(m-*it) && (m-*it!=*it || s.count(*it)>=2)){
              			printf("%d %d",*it,m-*it);
              			return 0;
              		}
              	puts("No");
              	return 0;
              }
              
              //lower_bound、upper_bound from <algorithm> 
              //@RRR (C24zhaozicheng)
              #include <iostream>
              #include<algorithm> 
              using namespace std;
              int a[100010];
              int cnt[100010];
              int main(){
              	int n;
              	cin>>n;
              	for(int i=1;i<=n;i++){
              		cin>>a[i];
              	}
              	int m;
              	cin>>m;
              	sort(a+1,a+1+n);
              	for(int i=1;i<=n;i++){
              		int w1=lower_bound(a+1,a+n+1,m-a[i])-a;
              		if(a[w1]==m-a[i]){
              			cout<<min(m-a[i],a[i])<<" "<<max(m-a[i],a[i]);
              			return 0;
              		}
              	}
              	cout<<"No";
              	return 0;
              } 
              */
              
              
              • 0
                @ 2024-2-23 20:13:45
                #include <bits/stdc++.h>
                using namespace std;
                const int maxn = 1e5 + 5;
                int a[maxn];
                
                
                
                int main(){
                
                int n,m;
                cin>>n;
                for(int i=0;i<n;i++) cin>>a[i];
                cin>>m;
                sort(a,a+n);
                
                {
                    for(int i=0;i<n;i++){
                    int liu=m-a[i];
                    if(binary_search(a+i+1,a+n,liu)){
                    cout<<a[i]<<" "<<liu;
                    return 0;
                  
                    }
                  
                    }
                    cout << "No" << endl;
                
                }
                }
                
                
                • 0
                  @ 2024-1-25 11:50:21

                  AC呆马:

                  #include<bits/stdc++.h>
                  using namespace std;
                  const int maxn = 1e5 + 5;
                  int a[maxn];
                  int SB(int l, int r, int p)
                  {
                  	int L = l;
                  	int R = r;
                  	while (L <= R)
                  	{
                  		int mid = L + (R - L) / 2;
                  		if(p == a[mid])
                  			return mid;
                  		else if(p > a[mid])
                  			L = mid + 1;
                  		else
                  			R = mid - 1;
                  	}
                  	return -1;
                  }
                  
                  int main()
                  {
                  	int n, m;
                  	cin >> n;
                  	for(int i = 0; i < n; i++)
                  		cin >> a[i];
                  	cin >> m;
                  	sort(a, a + n);
                  	for(int i = 0; i < n- 1; i++)
                  	{
                  		int res = m - a[i];
                  		if(SB(i+1,n-1,res) != -1)
                  		{
                  			cout << a[i] << " " << res;
                  			return 0;
                  		}
                  	}
                  	cout << "No" << endl;
                  	return 0;
                  }
                  
                  • -1
                    @ 2025-4-9 17:04:56

                    啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦 余X熙 & 刘X凡

                  • 1

                  Information

                  ID
                  729
                  Time
                  1000ms
                  Memory
                  256MiB
                  Difficulty
                  7
                  Tags
                  # Submissions
                  205
                  Accepted
                  40
                  Uploaded By