zoukankan      html  css  js  c++  java
  • 装箱问题【STL】

    7-9 装箱问题(20 分)

    假设有N项物品,大小分别为s​1​​、s​2​​、…、s​i​​、…、s​N​​,其中s​i​​为满足1≤s​i​​≤100的整数。要把这些物品装入到容量为100的一批箱子(序号1-N)中。装箱方法是:对每项物品, 顺序扫描箱子,把该物品放入足以能够容下它的第一个箱子中。请写一个程序模拟这种装箱过程,并输出每个物品所在的箱子序号,以及放置全部物品所需的箱子数目。
    输入格式:

    输入第一行给出物品个数N(≤1000);第二行给出N个正整数s​i​​(1≤s​i​​≤100,表示第i项物品的大小)。
    输出格式:

    按照输入顺序输出每个物品的大小及其所在的箱子序号,每个物品占1行,最后一行输出所需的箱子数目。
    输入样例:

    8
    60 70 80 90 30 40 10 20

    输出样例:

    60 1
    70 2
    80 3
    90 4
    30 1
    40 5
    10 1
    20 2
    5

    思路
    用 vector 来保存箱子其剩下的容量
    然后 第一次 push 一个 100进去
    然后 以后 每一次 都先查找
    有没有 一个能存放
    如果有 取数组下标最小的那个
    如果没有 新push 一个 100 进去
    然后 每次要保存 操作 要把物品 与对应数组下标对应起来
    下标 从1 开始计数
    最后再输出 v.size()

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-6;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e3 + 5;
    const int MOD = 1e9 + 7;
    
    struct Node
    {
        int x, y;
    }q[maxn];
    
    int main()
    {
        int n;
        cin >> n;
        vector <int> v;
        v.push_back(100);
        for (int i = 0; i < n; i++)
        {
            cin >> q[i].x;
            int flag = -1, Min = INF;
            int len = v.size();
            for (int j = 0; j < len; j++)
            {
                if (v[j] >= q[i].x && v[j] < Min)
                {
                    Min = v[j];
                    flag = j;
                    break;
                }
            }
            if (flag == -1)
            {
                v.push_back(100);
                v[v.size() - 1] -= q[i].x;
                q[i].y = v.size();
            }
            else
            {
                v[flag] -= q[i].x;
                q[i].y = flag + 1;
            }
        }
        for (int i = 0; i < n; i++)
        {
            printf("%d %d
    ", q[i].x, q[i].y);
        }
        cout << v.size() << endl;
    }
  • 相关阅读:
    asp.net :使用jquery 的ajax +WebService+json 实现无刷新去后台值
    如何清理数据库缓存
    如何在虚拟机中Linux+Oracle10gRAC安装
    ORA01031 权限不足存储过程中DBA 角色用户无法执行DDL
    如何查看存储过程执行计划
    如何查看执行计划
    如何使用tkprof
    C#位运算讲解与示例[转]
    C#中Invalidate() 方法
    如何创建强命名程序集, 如何查看强命名程序集的PublicKeyToken
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433224.html
Copyright © 2011-2022 走看看