编辑代码

#include <stdio.h>
#include <stdlib.h>
int Partition(int*,int,int);
void QuickSort(int*,int,int);
int main () {
    int w,n;
    int ans=0;
    int l,r;
    scanf("%d%d",&w,&n);
    int *v=(int*)malloc(n*sizeof(int));
    for(int i=0;i<n;i++){
        scanf("%d",&v[i]);
    }
    l=0,r=n-1;
    QuickSort(v,l,r);
    while(l<=r)
    {
        if(v[l]+v[r]<=w)   
          l++,r--,ans++;
        else
          r--,ans++;   
    }
    printf("%d",ans);
    return 0;
}

int Partition(int *A,int low,int high){
    int pivot=A[low];
    while(low<high){
        while(low<high && A[high]>=pivot) high--;
        A[low]=A[high];
        while(low<high && A[low]<=pivot) low++;
        A[high]=A[low];
    }
    A[low]=pivot;
    return low;
}
void QuickSort(int *A,int low,int high){
    if(low<high){
        int pivotpos=Partition(A,low,high);
        QuickSort(A,low,pivotpos-1);
        QuickSort(A,pivotpos+1,high);
    }
}