hdu 5835 虹软杯 1004 Danganronpa (贪心)

21 篇文章 0 订阅
2 篇文章 0 订阅

题目链接:hdu 5835

Danganronpa

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

Problem 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(T≤10) indicating the number of test cases.

Each case contains one integer n. The next line contains n (1≤n≤10) numbers: a1,a2,…,an, (1≤ai≤100000).

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
1
2
3 2

Sample Output
Case #1: 2

比起想策略的话题意要恶心一点,求满足条件的情况下最多可以摆几个桌子。

普通礼物就是从所有礼物中直接挑并且是要求相邻桌子的种类不能一样(也不许你跳着桌子放礼物),神秘礼物就是你随便选一个然后包起来不管他是什么种类的,没有不允许种类相同的要求。所以可以知道最多可以摆满礼物总数/2 个桌子。

策略是从小到大排序,然后把前n - 1的礼物总数num算出来(交叉放),如果最后数量小于礼物总数 / 2(保留整数)就再从第n种礼物中选num + 1个礼物插进去。

代码实现的策略有些不同,当时是考虑交叉放,每遍历一个种类就从后面的种类中挑出相同个数的礼物,n - 1种礼物遍历完后若还小于礼物总数 / 2 ,就继续从第n种礼物中选出最多能插入的礼物数量。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <algorithm>
#define MM 15
#define mod 1000000007
#define INF -0x3f3f3f3f
#define PI acos(-1)
#define ll long long

using namespace std;

int arr[MM];

int main()
{
    int T;
    scanf("%d", &T);
    for(int a = 1; a <= T; a++)
    {
        int n, total = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &arr[i]);
            total += arr[i];
        }//printf("total: %d\n", total);

        printf("Case #%d: ", a);
        if(n == 1 && arr[0] > 1)
        {
            printf("1\n");
            continue;
        }
        else if(n == 1 && arr[0] <= 1)
        {
            printf("0\n");
            continue;
        }
        sort(arr, arr + n);

        int Max = total / 2, num = 0;
        for(int i = 0; i < n - 1; i++)
        {
            num += arr[i] * 2;
            arr[i + 1] -= arr[i];
            //printf("%d ", arr[i]);
            if(num >= Max)
            {
                break;
            }
        }//printf("\n");
        if(num > Max)
            printf("%d\n", Max);
        else
        {
            num += num - arr[n - 2] * 2 + 1;
            if(num > Max)
                printf("%d\n", Max);
            else
                printf("%d\n", num);
        }
    }
    return 0;
}

场上a过。
当然我也就aa这题了。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值