Potions (Easy Version)(优先队列,反悔贪心)

使用优先队列解决药剂问题
该博客探讨了一个算法问题,即如何在初始生命值为0的情况下,从一排药剂中选择喝哪些,以确保生命值始终非负。通过遍历药剂,将负值药剂的绝对值放入优先队列,遇到生命值变为负的情况时,从队列中取出最大值恢复生命值。此策略保证了最大可饮用药剂数量。提供的AC代码展示了该算法的实现。

Potions (Easy Version)(优先队列,反悔贪心)

This is the easy version of the problem. The only difference is that in this version n≤2000. You can make hacks only if both versions of the problem are solved.

There are n potions in a line, with potion 1 on the far left and potion n on the far right. Each potion will increase your health by ai when drunk. ai can be negative, meaning that potion will decrease will health.

You start with 0 health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.

What is the largest number of potions you can drink?

Input
The first line contains a single integer n (1≤n≤2000) — the number of potions.

The next line contains n integers a1, a2, … ,an (−109≤ai≤109) which represent the change in health after drinking that potion.

Output
Output a single integer, the maximum number of potions you can drink without your health becoming negative.

Example
Input
6
4 -4 1 -3 1 -3

Output
5

Note
For the sample, you can drink 5 potions by taking potions 1, 3, 4, 5 and 6. It is not possible to drink all 6 potions because your health will go negative at some point

题意: 给个n,有n瓶药剂,可喝可不喝,初始生命值为0,喝下去,血量就+x(x可为负),保证每次喝完血量不为负,问最多能喝几瓶

思路: 遍历一遍,把负数的绝对值用优先队列(自动排序,从大到小)存起来,遇见就加,直到当前血量为负,就加队首,然后让队首出队,当前血量为负,最不应该喝的就是绝对值最大的负数,此时就反悔,不喝这瓶药就行了

AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int a[2100];
int main()
{
	long long n,i,j,k,x;
	priority_queue<int>q;//优先队列有自动排序的功能(从大到小)
	scanf("%lld",&n);
	long long sum=0;
	k=0;//记录个数
	for(i=0; i<n; i++)
	{
		scanf("%lld",&x);
		sum+=x;//遇见就加
		k++;
		if(x<0)//负数的绝对值存优先队列
			q.push(-x);
		if(sum<0)//血量为负,就反悔
		{

			j=q.top();//printf("%d+++\n",j);
			sum+=j;
			q.pop();
			k--;
		}
	}//	printf("%d----\n",sum);
	printf("%lld\n",k);
	return 0;
}
在MDPI LaTeX模板中添加功能模块,通常涉及对LaTeX类文件(如`mdpi.cls`)或文档中的特定配置进行修改。虽然MDPI模板本身并未提供名为 `potions` 的模块,但可以通过自定义宏包、定义新命令或修改模板源文件来实现类似功能的添加。 若需要添加类似于 `potions` 的模块,可以考虑以下方法: 1. **引入自定义宏包**:在LaTeX文档的导言区(`\documentclass` 和 `\begin{document}` 之间)使用 `\usepackage{}` 命令加载所需的宏包。例如,若 `potions` 涉及图形支持,可添加 `\usepackage{graphicx}` [^1]。 2. **定义新命令**:若 `potions` 指代某种特定功能(如自定义环境、格式化选项等),可以在导言区中使用 `\newcommand` 或 `\newenvironment` 来定义新的命令或环境。例如: ```latex \newcommand{\mycustomcommand}[1]{\textbf{#1}} ``` 上述命令定义了一个新的命令 `\mycustomcommand`,用于将输入文本加粗 [^1]。 3. **修改 `mdpi.cls` 文件**:若需对模板的核心功能进行扩展,可以直接编辑 `mdpi.cls` 文件。例如,可以添加新的条件判断、宏定义或环境配置。需要注意的是,在修改前应备份原始文件,并确保对LaTeX类文件的结构有一定了解 [^1]。 4. **使用 `etoolbox` 进行钩子插入**:如果希望在不直接修改模板文件的情况下注入代码,可以利用 `etoolbox` 宏包提供的钩子机制。例如,在文档开始时插入特定配置: ```latex \usepackage{etoolbox} \AtBeginDocument{ % 在此处添加自定义配置 } ``` 5. **参考MDPI官方文档**:MDPI通常会提供详细的模板说明文档,其中包含如何自定义和扩展模板功能的指导。建议查阅模板附带的 `.pdf` 说明文件,或访问 [MDPI Author Guidelines](https://www.mdpi.com/authors) 获取最新信息 [^1]。 ### 示例代码 以下是一个简单的示例,展示如何在MDPI模板中添加一个自定义命令 `\potions`,用于输出特定格式的内容: ```latex \documentclass[journal,article,submit,moreauthors,pdflatex]{mdpi} \usepackage{etoolbox} % 定义 \potions 命令 \newcommand{\potions}[1]{\textit{Potions: #1}} \begin{document} \section{Introduction} This is an example of using the \texttt{\textbackslash potions} command: \potions{elixir, tincture, draught}. \end{document} ``` 上述代码定义了一个新的命令 `\potions`,它接受一个参数并以斜体格式输出内容 [^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值