1 solutions

  • 0
    @ 2025-3-3 16:32:54

    C :

    #include<stdio.h>
    int a[30010],n,w,ans;
    void sort(int r,int t)
    {
    	int mid=a[(r+t)/2];
    	int i=r,j=t;
    	do
    	{
    		while(a[i]<mid)	i++;
    		while(a[j]>mid)	j--;
    		if(i<=j)
    		{
    			int e=a[i];
    			a[i]=a[j];
    			a[j]=e;
    			i++;
    			j--;
    		}
    	}while(i<=j);
    	if(i<t)	sort(i,t);
    	if(j>r)	sort(r,j);
    }
    int main()
    {
    	scanf("%d %d",&w,&n);
    	for(int i=1;i<=n;i++)
    	scanf("%d",&a[i]);
    	sort(1,n);
    	int i=1,j=n;
    	while(i<=j)
    	{
    		if(a[i]+a[j]<=w)
    		{
    			i++;
    			j--;
    		}
    		else
    		j--;
    		ans++;
    	}
    	printf("%d",ans);
    	return 0;
    }
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int n,a[30100],w;
    int main(){
    	int i,j,c = 0;
    	cin>>w>>n;
    	for(i = 0;i < n;i++){
    		cin>>a[i];
    	}
    	
    	sort(a,a+n);
    	i = 0,j = n - 1;
    	while(i <= j){
    		//不能放在一组中 
    		if(a[i] + a[j] > w){
    			j--;
    			c++;
    		}else{
    			i++;
    			j--;
    			c++;
    		}
    	}
    	
    	cout<<c<<endl;
    }
    
    
    
    • 1

    Information

    ID
    10480
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By