zoukankan      html  css  js  c++  java
  • CodeForce-799B T-shirt buying (STL_set)

    n 件T恤。第 i 件T恤的价格为 pi 。每个T恤有两面,第 i 件T恤正面颜色为 ai ,反面颜色为 bi

    m 个人想买T恤,每个人都恰好买一件。第 j 个人最喜欢颜色 cj

    一个人会选择买一件至少有一面颜色为他喜欢的颜色的,最便宜的T恤。如果没有合法的T恤,则不买。这些人按照给定的顺序来购买。

    你的任务是求出每个人会花多少钱。

    Input

    第一行包含一个整数 n (1 ≤ n ≤ 200 000) — 表示T恤的数量。

    之后一行为 p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), pi 表示第 i 件T恤的价格。

    之后一行为 a1, a2, ..., an (1 ≤ ai ≤ 3), ai 表示第 i 件T恤正面的颜色。

    之后一行为 b1, b2, ..., bn (1 ≤ bi ≤ 3), bi 表示第 i 件T恤背面的颜色。

    之后一行包含一个整数 m (1 ≤ m ≤ 200 000) — 表示消费者人数。

    之后一行为 c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj表示第 j 个消费者最喜欢的颜色。这些消费者会按输入的顺序一个一个来购买。

    Output

    一行 m 个数 — 第 j 个数表示第 j-th buyer will buy. If the j 个消费者需要花的钱数。如果不购买,输出 -1。

    Example
    Input
    5
    300 200 400 500 911
    1 2 1 2 3
    2 1 3 2 1
    6
    2 3 1 2 1 1
    Output
    200 400 300 500 911 -1 
    Input
    2
    1000000000 1
    1 1
    1 2
    2
    2 1
    Output
    1 1000000000 

    由于题目说了,每件衣服的价格都一定不同,所以可以用set。
    c++ stl容器set成员函数:insert()--在集合中插入元素。
    {
      Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.
      //通过插入新元素来扩展容器,通过插入的元素数量有效地增加容器大小。   Because elements in a set are unique, the insertion operation checks whether each inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).
      //因为集合中的元素是唯一的,所以插入操作检查每个插入的元素是否等同于已经在容器中的元素,如果是,则不插入元素,将迭代器返回到此现有元素(如果函数有返回值)。   Internally, set containers keep all their elements sorted following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering.
      //在内部,设置容器使其所有元素按照其比较对象指定的标准进行排序。 这些元件总是插入其相应的位置。
    }
    c++ stl容器set成员函数:empty()--如果集合为空,返回true。
    c++ stl容器set成员函数:begin()--返回指向第一个元素的迭代器。
    c++ stl容器set成员函数:erase()--删除集合中的元素。
    #include<bits/stdc++.h>
    using namespace std;
    int shirts[200050];
    int main()
    {
        int n;
        cin>>n;
        set<int> cost[4];
        for(int i=0;i<n;i++)   
        cin>>shirts[i];
        for(int i=0;i<2*n;i++)
        {
            int input;
            cin>>input;
            cost[input].insert(shirts[i%n]);
        }
        int m;
        cin>>m;
        while(m--)
        {
            int color;
            cin>>color;
            if(cost[color].empty()) cout<<"-1 ";
            else
            {
                int ans= *cost[color].begin();
                cout<<ans<<" ";
                for(int i=1;i<=3;i++)
                cost[i].erase(ans);
            }
        }
        cout<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    hdu 4407 Sum 容斥+当前离线
    2014第11周二开发记
    2014第11周一word样式
    2014第10周日
    2014第10周六
    2014第10周杂想
    2014第10周四项目开发回顾提纲
    2014第10周三高效程序员的习惯
    2014第10周二程序员思维
    2014第10周一
  • 原文地址:https://www.cnblogs.com/YingZhixin/p/6891113.html
Copyright © 2011-2022 走看看