算法训练 纪念品分组

ALGO-34 纪念品分组

资源限制

时间限制:1.0s 内存限制:256.0MB

问题描述

元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作。为使得参加晚会的同学所获得的纪念品价值 相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价格之和不能超过一个给定的整数。为了保证在尽量短的时 间内发完所有纪念品,乐乐希望分组的数目最少。
你的任务是写一个程序,找出所有分组方案中分组数最少的一种,输出最少的分组数目。

输入格式

输入包含n+2行:
第1行包括一个整数w,为每组纪念品价格之和的上限。
第2行为一个整数n,表示购来的纪念品的总件数。
第3~n+2行每行包含一个正整数pi (5 <= pi <= w),表示所对应纪念品的价格。

输出格式

输出仅一行,包含一个整数,即最少的分组数目。

测试样例

输入:
100
9
90
20
20
30
50
60
70
80
90

输出:
6

数据规模与约定

50%的数据满足:1 <= n <= 15
100%的数据满足:1 <= n <= 30000, 80 <= w <= 200

AC code:

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        int w = in.nextInt(), n = in.nextInt(), size = n;
        int[] list = new int[w + 1];
        while (n-- > 0) list[in.nextInt()]++;
        for (int k = 1, K = w / 2; k <= K; k++) {
            if (list[k] == 0) continue;
            list[k]--;
            for (int i = w - k; i > 0; i--) {
                if (list[i] == 0) continue;
                list[i]--;
                size--;
                k--;
                break;
            }
        }
        System.out.print(size);
    }

    static class InputReader {

        BufferedReader read;
        StringTokenizer token;
        String delimiters;

        InputReader(InputStream in) { this(in, " \t\n\r\f"); }

        InputReader(InputStream in, String delimiters) {
            this.read = new BufferedReader(new InputStreamReader(in));
            this.token = new StringTokenizer("", this.delimiters = delimiters);
        }

        String next() {
            while (!token.hasMoreTokens()) {
                try {
                    token = new StringTokenizer(read.readLine(), delimiters);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return token.nextToken();
        }

        int nextInt() { return Integer.parseInt(next()); }
    }
}

顺着这个思路优化了一下

这是目前最优版本,毕竟压缩了数据

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        int w = in.nextInt(), n = in.nextInt(), size = n;
        int[] list = new int[w + 1];
        while (n-- > 0) list[in.nextInt()]++;
        for (int i = 0, k = w / 2; i < k; i++, w--) {
            if (list[i] == 0) continue;
            if (list[i] > list[w]) {
                size -= list[w];
                list[i + 1] += list[i] - list[w];
            } else size -= list[i];
        }
        if ((list.length & 1) == 1) size -= list[w] / 2;
        else size -= (list[w] + list[w - 1]) / 2;
        System.out.print(size);
    }

    static class InputReader { . . . }
}

还有:

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        int w = in.nextInt(), n = in.nextInt(), cnt = 0;
        int[] list = new int[n];
        while (n-- > 0) list[n] = in.nextInt();
        Arrays.sort(list);
        for (int i = 0, j = list.length - 1; i <= j;) {
            if (list[i] + list[j] <= w) i++;
            cnt++;
            j--;
        }
        System.out.print(cnt);
    }

    static class InputReader { . . . }
}

在这里插入图片描述
以该章源码为顺序,三组一测,可以看到相比于在网络上(截止于20-08-26)能收集到的大多数方法,提升还是很可观的

虽然本质上大同小异

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值