• 51nod_learn_greedy_独木舟问题


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

    输入

    第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。
    接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。
    输出
     
    一行一个整数表示最少需要的独木舟数。
     
    输入示例

    3 6
    1
    2
    3

    输出示例

    2
     
    贪心的策略是...尽可能让多的船载两个人...
    我们可以先排序...
    然后从两边取.
    如果当前最大的能和最小的一起走,就一起走
    不能的话就自己走...
    然后直到所有人走完
     1 /*************************************************************************
     2     > File Name: code/51nod/learn/greedy/3.cpp
     3     > Author: 111qqz
     4     > Email: rkz2013@126.com 
     5     > Created Time: 2015年10月05日 星期一 19时42分15秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<iomanip>
    10 #include<cstdio>
    11 #include<algorithm>
    12 #include<cmath>
    13 #include<cstring>
    14 #include<string>
    15 #include<map>
    16 #include<set>
    17 #include<queue>
    18 #include<vector>
    19 #include<stack>
    20 #include<cctype>
    21                  
    22 #define yn hez111qqz
    23 #define j1 cute111qqz
    24 #define ms(a,x) memset(a,x,sizeof(a))
    25 using namespace std;
    26 const int dx4[4]={1,0,0,-1};
    27 const int dy4[4]={0,-1,1,0};
    28 typedef long long LL;
    29 typedef double DB;
    30 const int inf = 0x3f3f3f3f;
    31 const int N=1E4+5;
    32 LL a[N];
    33 int ans;
    34 int n;
    35 LL m;
    36 int main()
    37 {
    38  // #ifndef  ONLINE_JUDGE 
    39  //  freopen("in.txt","r",stdin);
    40 //  #endif
    41 
    42    scanf("%d %lld",&n,&m);
    43    for ( int  i  = 0 ; i < n ; i++) scanf("%lld",&a[i]);
    44    sort(a,a+n);
    45    int i = 0;
    46    int j = n-1;
    47    int cnt = 0;
    48    int ans = 0 ;
    49    
    50    while (cnt<n&&i<=j)
    51    {
    52        if (i==j)
    53        {
    54        cnt++;
    55        ans++;
    56        break;
    57        }
    58     
    59        if (a[i]+a[j]<=m)
    60        {
    61        i++;
    62        j--;
    63        cnt = cnt + 2;
    64        ans++;
    65        }
    66        else
    67        {
    68        j--;
    69        cnt = cnt + 1;
    70        ans++;
    71        }
    72    }
    73    printf("%d
    ",ans);
    74   
    75    
    76 // #ifndef ONLINE_JUDGE  
    77 //  fclose(stdin);
    78 //  #endif
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    ArcGIS Server TileLayer 跨域读取
    dojo.declare 未定义
    注册部署SOE, 提交SOE只能在IE浏览器中
    在maptalks中加载三维模型obj,fbx,glb
    三维模型 obj 转化为 three Json 文件格式
    leaflet map 地图初始化不能铺满div
    查找进行的过程中被停止 解决办法
    逆向的第一个小代码
    编码不规范导致的错误
    android4.4.2 短信广播变更
  • 原文地址:https://www.cnblogs.com/111qqz/p/4856186.html
Copyright © 2020-2023  润新知