51nod1432 独木舟


基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
Input
第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。
接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。
Output
一行一个整数表示最少需要的独木舟数。
Input示例
3 6
1
2
3
Output示例
2

好水的题....居然n方暴力都能过

本来还担心时间 想着是不是给每一个最大的体重的人都找尽量大的伙伴 如果找到两个相邻的时候 剩下的也不用找了肯定能配对的

但是emm好像写错了 因为剩下的人里面有的也已经配对了

就试了一下暴力居然过了。

OKfine

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;

const int maxn = 100005;
ll n, m;
ll wei[maxn];
bool onboat[maxn];

int main()
{
    while(scanf("%I64d%I64d",&n,&m) != EOF){
        memset(wei, 0, sizeof(wei));
        for(int i = 1;i <= n; i++){
            scanf("%I64d",&wei[i]);
        }
        memset(onboat, 0, sizeof(onboat));
        int cnt = 0;

        sort(wei + 1, wei + 1 + n);
        bool flag = false;
        for(int i = n; i >= 1; i--){
            if(onboat[i]) continue;
            onboat[i] = true;
            cnt++;
            for(int j = i - 1; j >= 1; j--){
                if(onboat[j]) continue;
                if(wei[i] + wei[j] <= m){
                    /*if(j == i - 1){
                        flag = true;
                    }*/
                    onboat[j] = true;
                    break;
                }
            }
            /*if(flag){
                if((i - 2) % 2){
                    cnt += (i - 2) / 2 + 1;
                }
                else
                    cnt += (i - 2) / 2;
                break;
            }*/
        }

        cout<< cnt<< endl;
    }
    return 0;
}




原文地址:https://www.cnblogs.com/wyboooo/p/9643446.html