NYOJ 71 独木舟上的旅行

原题链接

简单题,只要将乘客体重排序就好办了。

附ac代码:

#include <stdio.h>
#include <stdlib.h>
int a[301];

int cmp(const void *a, const void *b){
	return *(int *)a - *(int *)b;
}

int main(){
	int t, w, n, x, count;
	scanf("%d", &t);
	while(t-- && scanf("%d%d", &w, &n)){
		x = n;
		while(x--) scanf("%d", &a[x]);
		qsort(a, n, sizeof(int), cmp);
		count = n;
		int i = 0, j = n - 1;
		while(i < j){
			if(a[i] + a[j] <= w)
				--count, ++i, --j;
			else --j;
		}
		printf("%d\n", count);
	}
	return 0;
}


posted on 2014-02-12 00:38  长木Qiu  阅读(110)  评论(0编辑  收藏  举报