• 杂题 CF799B


    题目如下

    一包共 nn 件T恤被送到了商店。每件T恤被三个正整数 pipi , aiai 和 bibi 所描述。 pipi 是第 ii 件T恤的价格, aiai 是第 ii 件T恤正面的颜色, bibi 是第 ii 件T恤背面的颜色。所有 pipi 都各不相同, aiai 和 bibi 都是1~3的整数。

    有 mm 个顾客,每人都仅想买一件T恤。第 ii 个顾客最喜欢的颜色是 cjcj 。

    如果一件T恤至少一面(前或后)的颜色是顾客喜欢的,他才会将其买下;如果有多件T恤符合条件,他会选择最便宜的;如果没有符合条件的,他什么都不会买。我们假定顾客都乖巧地排着队一个一个来,前一个顾客走后第二个才能被接待。

    你需要计算每个顾客分别花了多少钱。

    输入格式:

     

    The first line contains single integer nn ( 1n2000001<=n<=200000 ) — the number of t-shirts.

    The following line contains sequence of integers p1p2...pnp1,p2,...,pn ( 1pi10000000001<=pi<=1000000000 ), where pipiequals to the price of the ii -th t-shirt.

    The following line contains sequence of integers a1a2...ana1,a2,...,an ( 1ai31<=ai<=3 ), where aiai equals to the front color of the ii -th t-shirt.

    The following line contains sequence of integers b1b2...bnb1,b2,...,bn ( 1bi31<=bi<=3 ), where bibi equals to the back color of the ii -th t-shirt.

    The next line contains single integer mm ( 1m2000001<=m<=200000 ) — the number of buyers.

    The following line contains sequence c1c2...cmc1,c2,...,cm ( 1cj31<=cj<=3 ), where cjcj equals to the favorite color of the jj -th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.

     

    输出格式:

     

    Print to the first line mm integers — the jj -th integer should be equal to the price of the t-shirt which the jj -th buyer will buy. If the jj -th buyer won't buy anything, print -1.

     

     

      这道题其实就是一个很简单的优先队列的调用。因为数据很大,单独循环会T。所以我们使用优先队列,并按照颜色的不同将结构体存入优先队列,每个人遍历一遍队列。由于价格从小到大,我们需要重载运算符。关于重载运算符的话,我此前学过很多次小詹的代码,但是总是记不住,今天试着用friend写一下,瞬间觉得充满了美感。

    1 friend bool operator < (Node x,Node y)
    2 
    3 {
    4   return x.minn < y.minn;
    5 }

     

    此题具体代码如下:

     1 #include<cstdio>
     2 #include<cmath>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 typedef long long ll;
     7 int read()
     8 {
     9     int a = 0, b = 1;
    10     char c = getchar();
    11     while(c < '0' or c > '9')
    12     {
    13         if(c == '-') b = -1;
    14         c = getchar();
    15     }
    16     while(c >= '0' and c <= '9')
    17     {
    18         a = a*10 + c - '0';
    19         c = getchar();
    20     }
    21     return a*b;
    22 }
    23 ll a[100005],b[100005],p[100005],n,vis[100005],t,m;
    24 struct node
    25 {
    26     int a,b,num,p;
    27     bool operator < (const node & other)const
    28     {
    29         return p > other.p || (p == other.p && num > other.num);
    30     }
    31 }e[10000005];
    32 int main()
    33 {
    34     priority_queue<node>k[4];
    35     n = read();
    36     for(int i=1; i<=n; i++) e[i].num = i;
    37     for(int i=1; i<=n; i++) e[i].p = read();
    38     for(int i=1; i<=n; i++) {e[i].a = read();k[e[i].a].push(e[i]);}
    39     for(int i=1; i<=n; i++) {e[i].b = read();k[e[i].b].push(e[i]);}
    40     m = read();
    41     for(int i=1; i<=m; i++)
    42     {
    43         t = read();
    44         if(k[t].empty())
    45         {
    46             printf("-1
    ");
    47             continue;
    48         }
    49         int c = k[t].top().num;
    50         while(vis[c] == 1)
    51         {
    52             if(k[t].empty())
    53             {
    54                 break;
    55             }
    56             k[t].pop();
    57             c = k[t].top().num;
    58         }
    59         if(!k[t].empty())
    60         {printf("%d ",e[c].p);k[t].pop();}
    61         else
    62         printf("-1
    ");
    63         vis[c] = 1;
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    sessionid如何产生?由谁产生?保存在哪里?
    springmvc原理
    java基础面试题
    mysql数据库去重复
    git安装和初次使用
    String的按值传递,java传参都是传值
    sublime Text3使用笔记
    git命令使用记录
    Git:错误:error:src refspec master does not match any
    java并发编程实战学习(3)--基础构建模块
  • 原文地址:https://www.cnblogs.com/qmcp/p/9410386.html
Copyright © 2020-2023  润新知