标签:skill rem des eof cto integer ola 技术 The
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).
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.
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.
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
4 -1 3 2 -1 1
这题 转化一下思路其实非常好写, vector很好用 ,
先进行一下二进制转化 , 然后可以发现 与 运算非常好用
因为 111111 最多才63 可以暴力用一个vector【70】维护
然后排序 ,每次都找下标最大的 ,然后erase掉 (这就是vector舒服的地方了)
其实和优先队列的思想差不多
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 #include <vector> 6 using namespace std; 7 const int maxn = 1e5 + 10; 8 9 char cake[6][3 * maxn], peo[6][maxn]; 10 int a[3 * maxn], b[maxn]; 11 vector<int>p[70]; 12 int main() { 13 int n, m; 14 while(scanf("%d%d", &n, &m) != EOF) { 15 for (int i = 0 ; i < 70 ; i++) p[i].clear(); 16 for (int i = 0 ; i < 6 ; i++) 17 scanf("%s", cake[i]); 18 for (int i = 0 ; i < n ; i++) 19 a[i] = (cake[0][i] - ‘0‘) * 1 + (cake[1][i] - ‘0‘) * 2 + (cake[2][i] - ‘0‘) * 4 + (cake[3][i] - ‘0‘) * 8 + (cake[4][i] - ‘0‘) * 16 + (cake[5][i] - ‘0‘) * 32; 20 for (int i = 0 ; i < 6 ; i++) 21 scanf("%s", peo[i]); 22 for (int i = 0 ; i < m ; i++) 23 b[i] = (peo[0][i] - ‘0‘) * 1 + (peo[1][i] - ‘0‘) * 2 + (peo[2][i] - ‘0‘) * 4 + (peo[3][i] - ‘0‘) * 8 + (peo[4][i] - ‘0‘) * 16 + (peo[5][i] - ‘0‘) * 32; 24 for (int i = 0 ; i < n ; i++) 25 p[a[i]].push_back(i + 1); 26 for (int i = 0 ; i < 64 ; i++) 27 sort(p[i].begin(), p[i].end()); 28 for (int i = 0 ; i < m ; i++) { 29 int ans = -1, idx; 30 for (int j = b[i] ; j < 64 ; j++) { 31 if ( (j & b[i]) != b[i] ) continue; 32 if (p[j].size() == 0) continue; 33 if (ans < p[j][p[j].size() - 1]) { 34 ans = p[j][p[j].size() - 1]; 35 idx = j; 36 } 37 } 38 printf("%d ", ans); 39 if (ans != -1) p[idx].erase(p[idx].end() - 1); 40 } 41 printf("\n"); 42 } 43 return 0; 44 }
标签:skill rem des eof cto integer ola 技术 The
原文地址:https://www.cnblogs.com/qldabiaoge/p/9065075.html