zoukankan      html  css  js  c++  java
  • 51Nod 独木舟(贪心)

    n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?

    Input
    第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。
    接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。
    Output
    一行一个整数表示最少需要的独木舟数。
    Input示例
    3 6
    1
    2
    3
    Output示例
    2
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <map>
    #include <set>
    using namespace std;
    #pragma comment(linker, "/stck:1024000000,1024000000")
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>=y?x:y)
    #define min(x,y) (x<=y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define pi acos(-1.0)
    #define ei exp(1)
    #define PI 3.1415926535897932384626433832
    #define ios() ios::sync_with_stdio(true)
    #define INF 0x3f3f3f3f
    #define mem(a) ((a,0,sizeof(a)))
    typedef long long ll;
    int n,m;
    int a[10006];
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        int l=0,r=n-1;
        int ans=0;
        while(l<=r)
        {
            if(a[l]+a[r]<=m) l++;
            r--;
            ans++;
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    java学习day62-Spring boot整合Shiro配置
    java学习day62-springboot中的拦截
    java学习day62-DB项目-首页菜单动态显示
    疯狂学java的第26天
    疯狂学java的第25天
    疯狂学java的第24天
    疯狂学java的第23天
    疯狂学java的第22天
    疯狂学java的第21天
    疯狂学java的第20天
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8974735.html
Copyright © 2011-2022 走看看