zoukankan      html  css  js  c++  java
  • 51nod 1432 独木舟 贪心

    基准时间限制: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


    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INF     0x3f3f3f3f
    #define INFLL   0x3f3f3f3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    typedef pair<int, int> PII;
    using namespace std;
    
    int n;
    LL m;
    
    LL a[10005];
    int vis[10005];
    
    int main() {
        //FIN
        while(~scanf("%d%lld", &n, &m)) {
            for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
            sort(a + 1, a + 1 + n);
            int ans = 0;
            memset(vis, 0, sizeof(vis));
            for(int i = n; i >= 1; i--) {
                if(vis[i]) continue;
                ans++;
                vis[i] = 1;
                LL tmp = m;
                tmp -= a[i];
                for(int j = n; j >= 1; j--) {
                    if(tmp >= a[j] && !vis[j]) {
                        vis[j] = 1;
                        break;
                    }
                }
            }
            printf("%d
    ", ans);
    
        }
        return 0;
    }
    

      

  • 相关阅读:
    Django缓存大总结
    Django之视图 ListView
    Django中间件之加载分析
    Django启动过程之超级详细分析
    Django中间件
    RabbitMq与Celery应用示意图
    爬虫的基本原理
    RestFramework的过滤组件 和 分页组件
    python注释、输入格式化输出输出及数据类型
    编程语言的发展历史及python的初了解
  • 原文地址:https://www.cnblogs.com/Hyouka/p/7425441.html
Copyright © 2011-2022 走看看