NYOJ 71 乘船问题【贪心】
时间复杂度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 乘船问题【贪心】的更多相关文章
- NYOJ 71 独木舟上的旅行【贪心】
解题思路:给出船的最大载重量w,和n个人,每只船最多可以乘坐两个人,问怎样坐船使得安排的船只的数量最少.这n个人的体重为a1,a2,a3,---,an-1,an首先将体重按升序排列好,再考虑最重的人, ...
- nyoj 71 独木舟上的旅行(贪心专题)
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...
- NYOJ 71 独木舟上的旅行(贪心)
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...
- nyoj 364 田忌赛马(贪心)
田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Here is a famous story in Chinese history. "That ...
- nyoj 71 独木舟上的旅行
点击打开链接 独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度: 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人 ...
- nyoj 364——田忌赛马——————【贪心】
田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Here is a famous story in Chinese history. "That ...
- [nyoj]会场安排问题-贪心
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- NYOJ 203 三国志(Dijkstra+贪心)
三国志 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 <三国志>是一款非常经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.如今他把游戏简化一下 ...
- nyoj 16-矩形嵌套(贪心 + 动态规划DP)
16-矩形嵌套 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:13 submit:28 题目描述: 有n个矩形,每个矩形可以用a,b来描述,表示长和 ...
随机推荐
- DOS程序员手册(十五)
837页 writeln('TRACING Current Buffer==='); holdup; bcbtrc(cvtbase^.curbfr); writeln; holdup ; writel ...
- csdn回到顶端
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- selenium启动IE浏览器报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode mu
意思是浏览器的保护模式设置不一致所导致 解决方案-->修改IE设置 将所有区域的保护模式勾选去掉即可
- Python学习-day17 jQuery method and demo
一:介绍: jQuery:是DOM和js的封装.jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).现在大多数的pc端的网站都 ...
- Python3.7在win10下安装PyAudio库以及实现音频的录制与播放
Python3.7 无法安装pyaudio 度娘的结果基本都是这个,pip install pyaudio.....然而十有八九你的电脑不买账,会报错. 报错信息: running install r ...
- 孤荷凌寒自学python第三十六天文件内容的迭代操作
孤荷凌寒自学python第三十六天python的文件操作对文件内容的迭代操作 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.os模块的其它文件操作方法补充 1 os.remove(文件 ...
- 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块
孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...
- (原)Unreal 渲染模块引言Temp
@author:白袍小道 引言 本文只在对Unreal渲染模块做一些详细的理解,务求能分析出个大概. 其中框架的思想和实现的过程,是非常值得学习和推敲一二的. 涉及资源系统,材 ...
- shell之dialog提示窗口
dialog 提示窗口 1.msgbox dialog --msgbox text 20 10 2.yesno dialog --title "Please answer&q ...
- C# 枚举相关操作——解析,遍历
我们在开发过程中,有时会让枚举作为数据源来绑定一些下拉列表,这时就需要解析枚举名称和值 . 先看一个枚举的定义: /// <summary> /// 数据库类型 /// </summ ...