时间复杂度O(n)

有n个人,第i个人的重量为w[i],每艘船的最大载重量均为c,且最多只能乘两个人。用最少的船装载所有人。

思路:从最轻的开始考虑,让最轻的和最重的一条船,若超出重量则可判定最重的只能一人一条船

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + ;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int n,m;
int a[maxn];
int main()
{
int t;
cin>>t;
while(t--){
memset(a,,sizeof(a));
int w,cnt=;
cin >> w >> n; //人数、载重量
for(int i=; i<n; i++) cin>>a[i];
sort(a,a+n);
int i=,j=n-;
while(i<=j)
{
if(a[i]+a[j]>w){
cnt++;
j--;
}
else{
cnt++;
i++;
j--;
}
}
cout << cnt << endl;
}
}

NYOJ 71 乘船问题【贪心】的更多相关文章

  1. NYOJ 71 独木舟上的旅行【贪心】

    解题思路:给出船的最大载重量w,和n个人,每只船最多可以乘坐两个人,问怎样坐船使得安排的船只的数量最少.这n个人的体重为a1,a2,a3,---,an-1,an首先将体重按升序排列好,再考虑最重的人, ...

  2. nyoj 71 独木舟上的旅行(贪心专题)

    独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...

  3. NYOJ 71 独木舟上的旅行(贪心)

    独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...

  4. nyoj 364 田忌赛马(贪心)

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  5. nyoj 71 独木舟上的旅行

    点击打开链接 独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度: 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人 ...

  6. nyoj 364——田忌赛马——————【贪心】

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  7. [nyoj]会场安排问题-贪心

    会场安排问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...

  8. NYOJ 203 三国志(Dijkstra+贪心)

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 <三国志>是一款非常经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.如今他把游戏简化一下 ...

  9. nyoj 16-矩形嵌套(贪心 + 动态规划DP)

    16-矩形嵌套 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:13 submit:28 题目描述: 有n个矩形,每个矩形可以用a,b来描述,表示长和 ...

随机推荐

  1. PJSIP-PJLIB-Socket

    As we all know,most our projects are need to use the socket to programme.Use socket we can connect o ...

  2. linux下多线程断点下载工具-axel

    今天要下载一下14G左右的文件,用wget约10小时,后来发现linux下有个多线程支持断点续传的下载工具axel,试了一下,下载速度大大增加. 包地址:http://pkgs.repoforge.o ...

  3. 【APUE】Chapter9 Process Relationships

    这一章看的比较混乱,可能是因为例子少:再有就是,这一章就是一个铺垫的章节. 9.2 terminal logins 啥叫termnial? 我感觉书上的terminal指的更可能是一些物理设备(key ...

  4. ASP.NET Core 认证与授权[1]:初识认证 (笔记)

    原文链接:  https://www.cnblogs.com/RainingNight/p/introduce-basic-authentication-in-asp-net-core.html 在A ...

  5. Entity Framework(四)--EF原理和状态管理

    一.原理: 如何查看真正执行的SQL是怎样的? DbContext有一个Database属性,Database属性有一个Log属性,是Action委托类型其中的参数就是sql语句,每次EF执行sql语 ...

  6. Python——数据类型之str

    本篇主要内容: 1.str元素的访问 2.str内置43个方法使用示例 str就是储存各种东西的字符串. 他的方法也是最多的...有各种各样的神奇魔法. 1.str的访问,str可以想列表那样访问每一 ...

  7. hadoop2.5.2学习及实践笔记(一)—— 伪分布式学习环境搭建

    软件 工具:vmware 10 系统:centOS 6.5  64位 Apache Hadoop: 2.5.2  64位 Jdk:  1.7.0_75  64位 安装规划 /opt/softwares ...

  8. msvs命令行编译lua5.3.4

    msvs命令行编译lua5.3.4 vslua.bat @echo off md bin md lib md include cd src cl /c /nologo /W3 /O2 /Ob1 /Oi ...

  9. 【SPOJ220】Relevant Phrases of Annihilation (SA)

    成功完成3连T!   嗯没错,三道TLE简直爽到不行,于是滚去看是不是模版出问题了..拿了3份其他P党的模版扔上去,嗯继续TLE...蒟蒻表示无能为力了... 思路像论文里面说的,依旧二分长度然后分组 ...

  10. ACdream 1023 抑或

    Xor Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statisti ...