hdu-5835-续缘之路

本文介绍了一个关于礼物分配的问题,模拟了在一个无限数量的学生中,如何根据特定规则分配有限种类的礼物,使得尽可能多的学生能够得到礼物。文章提供了一种使用队列进行模拟的方法,并给出了解决方案。

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

Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Li

Description 
Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist of n kinds with a[i] quantities of each kind, for her students and wants to hold a class meeting. Because of the busy work, she gives her gifts to the monitor, Chiaki Nanami. Due to the strange design of the school, the students’ desks are in a row. Chiaki Nanami wants to arrange gifts like this:

  1. Each table will be prepared for a mysterious gift and an ordinary gift.

  2. In order to reflect the Chisa Yukizome’s generosity, the kinds of the ordinary gift on the adjacent table must be different.

  3. There are no limits for the mysterious gift.

  4. The gift must be placed continuously.

She wants to know how many students can get gifts in accordance with her idea at most (Suppose the number of students are infinite). As the most important people of her, you are easy to solve it, aren’t you?

Input 
The first line of input contains an integer T(T10) indicating the number of test cases.

Each case contains one integer n. The next line contains n (1n10) numbers: a1,a2,...,an(1ai100000).

Output 
For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the answer of Chiaki Nanami’s question.

Sample Input 


3 2

Sample Output 
Case #1: 2

具体做法:维护一个队列

首先可以知道答案不超过所有礼物之和的二分之一(重要!!!),那么通过一个队列来模拟发礼物的过程,将每个礼物的个数放入队列,然后弹出一个判断两个条件:1.如果大于1就减去1然后放入队列尾部,然后将结果加一。2.队列里是否还有下一个元素,也就是要满足题目中不同礼物相隔这个条件。


最后判断如果结果大于所有礼物之和的二分之一,那说明每一个人都能按题意拿到普通礼物,剩下的就都是神秘礼物就可以了。

AC代码:(未优化)

</pre><pre>
import java.io.*;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Queue<Integer>queue=new LinkedBlockingQueue<Integer>();
        for(int o=1;o<=n;o++)
        {
            int k=sc.nextInt();
            int sum=0;
            queue.clear();
            for(int i=1;i<=k;i++)
            {
                int cur=sc.nextInt();
                queue.add(cur);
                sum=sum+cur;
            }
            int count=0;
            while(!queue.isEmpty())
            {
                int cur1=queue.poll();
                count++;
                if(queue.isEmpty())
                {
                    break;
                }
                if(cur1>1)
                queue.add(cur1-1);
                int cur2=queue.poll();
                count++;
                if(cur2>1)
                queue.add(cur2-1);
            }
            System.out.print("Case #"+o+": ");
            System.out.println(Math.min(sum/2, count));
        }
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值