zoukankan      html  css  js  c++  java
  • T-shirts Distribution

    T-shirts Distribution
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXLyou are given the number of t-shirts of this size.

    During the registration, the organizers asked each of the n participants about the t-shirt size he wants. If a participant hesitated between two sizes, he could specify two neighboring sizes — this means that any of these two sizes suits him.

    Write a program that will determine whether it is possible to present a t-shirt to each participant of the competition, or not. Of course, each participant should get a t-shirt of proper size:

    • the size he wanted, if he specified one size;
    • any of the two neibouring sizes, if he specified two sizes.

    If it is possible, the program should find any valid distribution of the t-shirts.

    Input

    The first line of the input contains six non-negative integers — the number of t-shirts of each size. The numbers are given for the sizesS, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100 000.

    The second line contains positive integer n (1 ≤ n ≤ 100 000) — the number of participants.

    The following n lines contain the sizes specified by the participants, one line per participant. The i-th line contains information provided by the i-th participant: single size or two sizes separated by comma (without any spaces). If there are two sizes, the sizes are written in increasing order. It is guaranteed that two sizes separated by comma are neighboring.

    Output

    If it is not possible to present a t-shirt to each participant, print «NO» (without quotes).

    Otherwise, print n + 1 lines. In the first line print «YES» (without quotes). In the following n lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants should be the same as in the input.

    If there are multiple solutions, print any of them.

    Examples
    input
    0 1 0 1 1 0
    3
    XL
    S,M
    XL,XXL
    output
    YES
    XL
    M
    XXL
    input
    1 1 2 0 1 1
    5
    S
    M
    S,M
    XXL,XXXL
    XL,XXL
    output
    NO
    分析:因为1个人最多选两件型号相邻的T恤,所以直接从小到大的型号贪心即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<ll,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,c[7];
    struct node
    {
        string a,b;
        int ca;
    }op[maxn];
    string d;
    bool flag=true;
    string ca[7]={"S","M", "L", "XL", "XXL", "XXXL"};
    int gao(string a)
    {
        int i;
        rep(i,0,5)if(a==ca[i])return i+1;
    }
    vi v[7];
    int main()
    {
        int i,j;
        rep(i,1,6)scanf("%d",&c[i]);
        scanf("%d",&n);
        rep(i,1,n)
        {
            cin>>d;
            int xx=0;
            for(j=0;d[j];j++)
            {
                if(d[j]==',')
                {
                    xx=j;
                    break;
                }
            }
            if(xx)
            {
                op[i].a=d.substr(0,xx);
                op[i].b=d.substr(xx+1);
                v[gao(d.substr(0,xx))].pb(i);
            }
            else
            {
                if(--c[gao(d)]<0)
                {
                    flag=false;
                    break;
                }
                op[i].a=d;
                op[i].ca=1;
            }
        }
        for(i=1;i<=5;i++)
        {
            for(int x:v[i])
            {
                if(c[i]>0)
                {
                    c[i]--;
                    op[x].ca=1;
                }
                else if(c[i+1]>0)c[i+1]--,op[x].ca=2;
                else
                {
                    flag=false;
                    break;
                }
            }
        }
        if(flag)
        {
            puts("YES");
            rep(i,1,n)
            {
                if(op[i].ca==1)cout<<op[i].a<<endl;
                else cout<<op[i].b<<endl;
            }
        }
        else puts("NO");
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    P/Invoke .NET调用win32API
    怎么将字节流转换成汉字?(硬件printf的汉字怎么解析?)
    个人电脑配置FTP服务器,四张图搞定。项目需要,并自己写了个客户端实现下载和上传的功能!
    C# 中Datetime类用法总结
    C#环境datagidview添加删除操作
    C#环境下,文本框翻屏,怎么一直显示当前插入的内容!!!!!!!!!!!!!!!!
    eclipse下提交job时报错mapred.JobClient: No job jar file set. User classes may not be found.
    SQL Server连接数据库失败,可能的问题!
    写好的mapreduce程序,编译,打包,得到最后的jar包! 验证jar包 ! 整体流程
    在虚拟机环境下,电脑间拷贝配置好的伪分布式Hadoop环境,出现namenode不能启动的问题!
  • 原文地址:https://www.cnblogs.com/dyzll/p/5965694.html
Copyright © 2011-2022 走看看