zoukankan      html  css  js  c++  java
  • 【9604】纪念品分组

    Time Limit: 3 second
    Memory Limit: 2 MB

    【问题描述】

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

    【输入格式】

    包含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
    

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=9604

    【题解】

    最小的那个纪念品应该和谁放在一起?
    应该和价值在中间的某个纪念品?
    如果可以和更大价值的纪念品放在一起,为什么不放呢?
    因为价值在中间的话,你完全可以再找个价值更大一点的放在一起,没必要就选择最小的,把最小价值的这个纪念品让给更大价值的人嘛;
    所以把纪念品从小到大排序;
    对于每个最大值都尝试让他和最小值结合;看看能不能结合,如果可以结合,那么这样肯定是最优的;(如果最大价值和最小值都没办法结合、那么这个最大值肯定就只能当孤家寡人了,把它一个人包装在一起,然后去尝试把最小值和一个比它小一点的值放在一起);
    因为价值在中间的话,你完全可以再找个价值更大一点的放在一起,没必要就选择最小的,把最小价值的这个纪念品让给更大价值(也就是尽可能最大价值的)的人嘛;

    【完整代码】

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    void rel(LL &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t) && t!='-') t = getchar();
        LL sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    void rei(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)&&t!='-') t = getchar();
        int sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    const int MAXN = 3e4+100;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int w,n;
    int a[MAXN];
    bool bo[MAXN];
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        memset(bo,true,sizeof(bo));
        scanf("%d",&w);
        scanf("%d",&n);
        rep1(i,1,n)
            scanf("%d",&a[i]);
        int ans = 0;
        int l = 1;
        sort(a+1,a+1+n);
        rep2(j,n,1)
            if (bo[j])
            {
                if (j > l)
                    if (a[j]+a[l]<=w)
                        bo[l] = false,l++;
                ans++;
            }
        cout << ans << endl;
        return 0;
    }
    
  • 相关阅读:
    GIT Bash 简单讲解git如何推/拉代码
    python os模块详细用法
    Python基础案例练习:制作学生管理系统
    Python函数中4种参数的使用
    python基础:try...except...的详细用法
    Python关于装饰器的练习题
    ELB HTTP监听器访问慢的问题
    花生壳 b.oray.com
    euler安装使用docker
    lvs配置会话超时时间
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626885.html
Copyright © 2011-2022 走看看