South Pacific Divisionals Bake Off

题目链接:点击打开链接

文章转载自:xuanqis.com

Description

Davy decided to start a weekend market stall where he sells his famous cakes. For the first market stall, Davy decided to bake n cakes. Each cake is described by its deliciousness and the flavours it contains, which is a (possibly empty) subset of the flavours {caramel, cherry, chocolate, cinnamon, coconut, cookies}. Because of Davy’s skill in baking, he has a line of m customers when he opens his stall who wish to buy a cake. Davy will serve them in the order they are lined up. Each customer has a required subset of flavours they would like in their cake, but are happy to receive additional flavours in their cake. Davy will give each customer the most delicious cake left that contains at least the flavours that the customer has asked for. You should help Davy determine which cake to sell to each customer (or if there is no cake that satisfies that customer’s requirements, in which case, they buy nothing).

Input

The first line contains two integers n (1 ≤ n ≤ 300 000), which is the number of cakes, and m (1 ≤ m ≤ 100 000), which is the number of customers. The next 6 lines describe the flavours contained in the cakes. The first of these lines contains a string of length n, which describes if caramel is in each cake. This string will contain a 1 in the ith position if cake i contains caramel and 0 otherwise. The second through sixth of these lines will describe cherry, chocolate, cinnamon, coconut and cookies, respectively, in the same format. The cakes are numbered from left to right, starting with cake 1 on the left. No two cakes have the same deliciousness and are sorted by their deliciousness, with cake 1 being the least delicious and cake n being the most delicious. The next 6 lines describe the flavours requested by the customers. The first of these lines contains a string of length m, which describes if each customer has requested caramel in their cake. This string will contain a 1 in the ith position if customer i requested caramel and 0 otherwise. The second through sixth of these lines will describe cherry, chocolate, cinnamon, coconut and cookies, respectively, in the same format.

Output

Display the number of the cake purchased by each customer in the order that they are requested. If a customer does not purchase a cake, display -1 for them instead.

Sample Input

4 2
0001
1111
0001
1111
0001
1111
01
11
01
11
01
11

3 4
000
000
000
010
101
110
0000
0000
0000
0010
1000
0100

Sample Output

4 -1

3 2 -1 1

Hint

Source
South Pacific Divisionals

题意

有n个蛋糕,m个顾客,顾客排队购买蛋糕。每个蛋糕可以具有六种属性,顾客购买具有一定要求的蛋糕,可以卖给顾客属性多于他要求的蛋糕,但不能少。当多个蛋糕满足顾客要求时,从后面的蛋糕卖起。就是将蛋糕标号为1-n,第n个和n-1个都满足要求,卖第n个给顾客。

思路

首先将蛋糕转化为十进制的数字,六种口味,总共才64种,所以用一个优先队列数组(vector也行)来将该类型蛋糕的序号存入,然后对顾客的需要进行匹配,枚举看哪些类型的蛋糕满足顾客的要求,其中满足要求则按位与顾客需要的蛋糕的值得到的值仍然是顾客需要蛋糕的值。得出最大的序号,删除该序号所在队列的最大值。

代码


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>

using namespace std;

priority_queue<int> que[64];  
char str[6][310000];  
int num;
int main(){
    freopen("3.txt", "r", stdin);  
    int n,m;  
    while(~scanf("%d%d", &n, &m)){
        for(int i=0;i<64;i++){  //清空队列 
            while(!que[i].empty())que[i].pop();  
        }
        for(int i=0;i<6;i++){  //输入蛋糕的数据 
            scanf("%s",str[i]);  
        }
        for(int i=0;i<n;i++){  //计算蛋糕的类型并存入队列 
            num=0;  
            for(int j=0;j<6;j++){
                if(str[j][i]=='1'){
                    num += (1<<j); 
                }
            }
            que[num].push(i+1);  
        }
        for(int i=0;i<6;i++){  //输入顾客的需要 
            scanf("%s",str[i]);  
        }
        for(int i=0;i<m;i++){
            int numa=-1;//记录要删除值的队列 
            int numb=-1;//记录下标值 
            num=0;  
            for(int j=0;j<6;j++){  
                if(str[j][i]=='1'){  
                    num += (1<<j); 
                }  
            }  
            for(int j=0;j<64;j++){  
                if((j&num)==num){  //该类蛋糕满足了顾客的需要 
                    if(!que[j].empty() && que[j].top()>numb){  //还有蛋糕,并且序号大于当前值 
                        numb=que[j].top();  
                        numa=j;  
                    }  
                }  
            }  
            if(numa!=-1)que[numa].pop();  //能满足顾客需要的情况才删除 
            printf("%d",numb);  //输出,如果不能满足,是初值-1 
            if(i==m-1)printf("\n");    
            else printf(" ");    
        }  
    }  
}  

全部评论

相关推荐

02-12 20:22
重庆大学 Java
字节暑期刚入职四天,因为是年前,所以很多正职都放假走了,也就没有给我分配mt,然后有一个老哥在我来的时候给我发了一个landing手册,然后还有关于部门业务的白皮书,还有一些业务代码。然后本人是java面的,进来第一次接触go语言&nbsp;前面几天熟悉了一下go的语法和go的框架,可以读但是还不太会写,然后业务白皮书也看的很头疼,包括landing手册里要了解的很多东西说实话我看文档真的快看死了,一个嵌套一个,问题是我还完全不知道咋用这个我了解的东西,还有就是那个项目代码,那个老哥喊我去写写单测,熟悉一下go的语法,但也进行的很困难(这是我第一段实习,之前都是springboot那一套,真不太熟悉这个)想问问大家的建议,就是我从现在开始到在开年回来之前应该做些什么,我目前就一个想法&nbsp;就是复现一个landing手册上的go框架小项目&nbsp;就是相当于帮自己锻炼锻炼怎么写go&nbsp;或者各位大佬有没有更好的锻炼go语法的建议还有就是大家都在说vibe&nbsp;coding,那我应该怎么锻炼自己使用ai的能力,感觉我除了给一些需求然后它给我生成代码,好像就没别的用法了,那些什么工作流、拆解、skill啥的都不知道从哪一个地方开始,包括我现在正在实习,不知道精力该怎么分配,去网上想找找关于agent开发的一些学习流程,说实话,众说纷纭,有的是从python开始打基础然后系统学那些rag&nbsp;prompt&nbsp;langchain&nbsp;mcp等等,有的是说直接找一个github上的ai项目然后反复问ai,我确实有点迷茫,恳求各位大佬能留下你们宝贵的建议,我一定认真反复深刻学习有一说一&nbsp;我觉得字节饭挺好吃的!
双非后端失败第N人:1. go语言我建议你让ai带着你先把基本语法速通了,然后再去用go重新刷你以前刷过的leetcode,这样熟悉起来很快 2. 直接看你们组go项目,里面用***比较复杂,然后把每一个语法现象都喂给ai,一点点看
字节跳动公司福利 1371人发布
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务