2531. 乘船

Andrew-Malcom

include

using namespace std;
int main()
{
        int t;cin>>t;
        while(t--){
                int n,k;cin>>n>>k;
                int a[n];
                for(int i=0;i<n;i++){
                        cin>>a[i];
                }
                sort(a,a+n);
                int j=0,p=n-1,ans=0;
                while(!(j>p)){
                        if(a[j]+a[p]<=k){
                                ans++;
                                j++;p--;
                        }
                        else if(a[j]+a[p]>k){
                                ans++;
                                p--;
                        }
                }
                cout<<ans<<endl;
        }
}
Revector
#include <stdio.h>
#include <stdlib.h>
#define MAX_PERSON 2001

int cmp(int* a, int* b){
    if (*a > *b) return 1;
    else if (*a < *b) return -1;
    else return 0;
}


int main(){
    int T;
    int weights[MAX_PERSON];
    scanf("%d", &T);

    while (T--){
        int person_n, carry;
        scanf("%d %d", &person_n, &carry);

        for (int i = 0; i < person_n; i++){
            scanf("%d", &weights[i]);
        }
        qsort(weights, person_n, sizeof(int), cmp);

        int lt = 0;
        int rt = person_n - 1;

        int boats = 0;
        while (lt < rt){
            if (weights[rt] + weights[lt] <= carry){
                boats++;
                lt++;
                rt--;
            } else {
                boats++;
                rt--;
            }
        }
        if (lt == rt) boats++;
        printf("%d\n", boats);
    }

    return 0;
}
你当前正在回复 博客/题目
存在问题!