Codeforces 545D Queue【贪心+模拟】

本文介绍了一个算法问题,即如何通过调整排队顺序来最大化顾客满意度。具体地,通过将顾客的忍耐时间排序并模拟排队过程,实现了求解最大数量满意顾客的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Queue
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little girl Susie went shopping with her mom and she wondered how to improve service quality.

There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.

Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.

Input

The first line contains integer n (1 ≤ n ≤ 105).

The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.

Output

Print a single number — the maximum number of not disappointed people in the queue.

Examples
Input
5
15 2 1 5 3
Output
4
Note

Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.


题目大意:

一共有N个人要排队,对应每个人如果等待的时间超过了自己的忍耐时间,那么对应这个人就会走,问怎样分配这些人进行排队能够使得最多的人得到服务。


思路(这个D好JB水啊):


直接将每个人的忍耐时间从小到大排序。

接下来我们模拟整个排队过程即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll __int64
ll a[100060];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%I64d",&a[i]);
        }
        int tmp=n;
        ll sum=0;
        sort(a,a+n);
        for(int i=0;i<n;i++)
        {
            if(sum<=a[i])
            {
                sum+=a[i];
            }
            else
            {
                tmp--;
            }
        }
        printf("%d\n",tmp);
    }
}








<think>嗯,用户想要寻找适合Codeforces新手的题目列表推荐。首先,我需要回忆一下Codeforces的题目分类和难度分级。通常,Codeforces的题目难度从A开始递增,A题通常是最简单的,适合新手。但可能用户需要更具体的指导,比如按主题或技巧分类的题目列表。 然后,我应该考虑用户可能的真实需求。作为新手,他们可能需要先掌握基本的数据结构、算法和编程基础,比如循环、条件语句、数组处理、字符串操作等。因此,推荐的题目应该覆盖这些基础内容,并且难度适中,避免太复杂的问题导致挫败感。 接下来,我需要回忆或查找一些经典的适合新手的题目。比如,Problem 4C Registration system,这是一个处理字符串和哈希映射的题目,非常适合练习基本的数据结构应用。另外,Problem 71A,处理字符串缩写,也是一个常见的入门题。其他像Problem 1A(数学计算)、Problem 158A(数组处理)、Problem 118A(字符串处理)都是常见的新手题。 还要考虑到不同知识点的覆盖,比如数学问题、贪心算法、简单动态规划等。例如,Problem 50A(棋盘覆盖问题)涉及数学和贪心策略,Problem 266B(队列操作)涉及模拟和基本算法思路。 然后,我需要验证这些题目的难度是否确实适合新手。比如,查看这些题目的通过率和标签,确认它们通常被归类为Div.2 A或B题,或者是教育轮次的早期题目。此外,可能需要推荐一些在线资源或整理好的题单,比如Codeforces的官方分类或用户整理的入门列表,这样用户可以系统地练习。 用户可能还需要一些学习建议,比如如何逐步提升,如何利用题解和讨论区,或者如何参加比赛。因此,在回答中加入这些建议会更有帮助。同时,要确保推荐的具体题目都有正确的编号和链接,方便用户查找。 最后,确保回答结构清晰,分点列出推荐的题目,并简要说明每个题目的考察点和难度,让用户一目了然。还要检查是否有遗漏的重要知识点,比如排序、搜索等基础算法,如果有的话需要补充进去。同时,提醒用户多练习和参加比赛,这是提升的关键。</think>### Codeforces新手题目推荐指南 #### 一、基础编程能力训练 1. **Problem 4C: Registration system** 考察哈希表应用,学习处理字符串计数问题。题目要求为重复用户名自动添加序号,适合练习`map`容器的使用[^1]。 2. **Problem 71A: Way Too Long Words** 练习字符串基本操作,规则简单:长度超过10的单词缩写为首字母+中间字母数+尾字母。 ```python n = int(input()) for _ in range(n): s = input() print(s[0] + str(len(s)-2) + s[-1] if len(s)>10 else s) ``` 3. **Problem 1A: Theatre Square** 基础数学题,计算铺地砖数量,涉及向上取整运算: $$ \text{数量} = \lceil \frac{n}{a} \rceil \times \lceil \frac{m}{a} \rceil $$ #### 二、算法思维入门 4. **Problem 158A: Next Round** 数组处理经典题,理解条件判断与边界值处理,注意并列分数的情况。 5. **Problem 50A: Domino piling** 贪心策略应用,通过数学推导直接得出公式: $$ \text{最大骨牌数} = \lfloor \frac{m \times n}{2} \rfloor $$ 6. **Problem 266B: Queue at the School** 模拟题,学习时间步进和队列状态更新,适合理解算法实现流程。 #### 三、数据结构专项 7. **Problem 118A: String Task** 字符串处理综合练习,需完成大小写转换、元音删除和字符插入操作。 8. **Problem 112A: Petya and Strings** 比较字符串的字典序,重点练习ASCII码处理和大小写统一方法。 #### 四、推荐练习路径 - **第一阶段**:优先完成1A、71A、266B,掌握输入输出和基础逻辑 - **第二阶段**:尝试4C、158A、118A,学习数据结构和字符串处理 - **进阶路线**:挑战动态规划入门题如Problem 189A(丝带切割问题) #### 五、学习资源推荐 1. Codeforces官方标签筛选:使用`tags`功能选择`brute force`/`math`标签的800-1000分题目 2. 知名训练清单:参考[Errichto的入门题单](https://codeforces.com/blog/entry/77173),包含分类训练指引 3. 虚拟参赛:通过Practice功能参与旧比赛的Div.2场次,重点关注A-B题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值