Codeforces Round #413 B T-shirt buying (STL set)
链接:http://codeforces.com/contest/799/problem/B
题意:
给定n件衣服,对于第i(1<i<=n)件衣服,分别有价格pi,前颜色ai,后颜色bi三个值。然后有m个顺序访问店铺的顾客,每位顾客有一个最喜欢的颜色,只要衣服有一面是他们最喜欢的颜色,他们就会买下这些衣服中最便宜的一件,分别求出每个顾客购买时的价格,如果没有衣服是他们最喜欢的颜色就输出-1。 n,m <= 2e5。
分析:
一开始做的时候看到时间限制3s,直接sort加对于m次询问的O(n)匹配,复杂度为nlogn+m*n,超时。
然后发现这题数据查找的复杂度应该更低才能过, 所以选用了set作为数据结构, 对于set,每次操作都是logn, 平均下来应该是nlogn + mlogn的复杂度,参考了tourist的代码,终于A了。
具体操作是开一个vis去标记哪些衣服被买过。
开3个颜色的set<pair<int,int> s[color],,然后把符合衣服的价钱和编号号作为一对pair插入,这时候set是按pair的first元素排序的。
每次都取x = s[color].begin().second(这个x的含义是,符合条件的最便宜衣服的编号),删除这个颜色set中的这个元素,如果能操作说明里面有元素,否则输出-1。
然后判断这个x代表的衣服是否已经被购买过,如果没有就输出这件价钱,标记为已购买,否则重复取取x = s[color].begin().second,直到输出或者元素被删完,删除其实是因为为了找下一个最小值。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int p[maxn], a[maxn], b[maxn];
int n, m;
set < pair <int, int> > s[];
bool vis[maxn];
int main()
{
//freopen("1.txt","r",stdin);
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d", &p[i]);
}
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
for(int i = ; i < n; i++)
{
scanf("%d", &b[i]);
}
for(int i = ; i < n; i++)
{
s[a[i]].insert(make_pair(p[i],i));//这个make_pair有点像初始化列表,就是把两个值构成pair然后insert到set中。
s[b[i]].insert(make_pair(p[i],i));//这是将一对 <价格,编号>的pair插入set,set按first(第一个元素——价格)排序
}
scanf("%d", &m);
memset(vis,,sizeof(vis));
while(m--)
{
int t,ans = -;
scanf("%d", &t);
while(!s[t].empty())
{
int x = (*(s[t].begin())).second;
s[t].erase(s[t].begin());
if(vis[x])
{
continue;
}
vis[x] = ;
ans = p[x];
break;
}
printf("%d ", ans);
}
puts("");
return ;
}
Codeforces Round #413 B T-shirt buying (STL set)的更多相关文章
- Codeforces Round #413 B. T-shirt buying
B. T-shirt buying time limit per test 3 seconds memory limit per test 256 megabytes A new pack ...
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)(A.暴力,B.优先队列,C.dp乱搞)
A. Carrot Cakes time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- Codeforces Round#413 Div.2
A. Carrot Cakes 题面 In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, al ...
- Codeforces Round#413 Problem A - C
Problem#A Carrot Cakes vjudge链接[here] (偷个懒,cf链接就不给了) 题目大意是说,烤面包,给出一段时间内可以考的面包数,建第二个炉子的时间,需要达到的面包数,问建 ...
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) 一夜回到小学生
我从来没想过自己可以被支配的这么惨,大神讲这个场不容易掉分的啊 A. Carrot Cakes time limit per test 1 second memory limit per test 2 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- Codeforces Round #413, rated, Div. 1 + Div. 2 C. Fountains(贪心 or 树状数组)
http://codeforces.com/contest/799/problem/C 题意: 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 ...
- C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)
题目链接:http://codeforces.com/contest/799/problem/C 题目: 题意: 给你n种喷泉的价格和漂亮值,这n种喷泉题目指定用钻石或现金支付(分别用D和C表示),C ...
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】
题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...
随机推荐
- [SRM625 Div1 Hard] Seatfriends
题目链接:Portal Vjudge Solution 一开始拿到这一题Sb了,把空放到dp中一起考虑了,这样计数就变得很麻烦. 其实我们可以把空位拿出来,假设它是存在的,最后再放回去. 那么就可以钦 ...
- [ZPG TEST 108] Antimonotonicity【贪心】
T2:Antimonotonicity (Antimonotonicity.pas/in/out 128M 1s) 给你1-N的一个排列,数列中的数字互不相等,要求找出最长的子序列a,满足a1> ...
- 洛谷 P3690 【模板】Link Cut Tree (动态树) || bzoj 3282: Tree
https://blog.csdn.net/saramanda/article/details/55253627 https://blog.csdn.net/CHHNZ/article/details ...
- 区间DP UVA 10453 Make Palindrome
题目传送门 /* 题意:问最少插入多少个字符使得字符串变成回文串 区间DP:dp[i][j]表示[l, r]的字符串要成为回文需要插入几个字符串,那么dp[l][r] = dp[l+1][r-1]; ...
- JetSpeed2部署至Apusic操作步骤记录
JetSpeed2部署至Apusic操作步骤记录. 1.创建Apusic新域,配置端口.管理端口和管理密码. 2.将Tomcat/lib目录中的所有jar包复制至Apusic域中的lib目录中(包括数 ...
- Service官方教程(9)绑定服务时的注意事项
Binding to a Service Application components (clients) can bind to a service by calling bindService() ...
- Android偏好设置(2)为应用定义一个偏好设置xml
1.Defining Preferences in XML Although you can instantiate new Preference objects at runtime, you sh ...
- Linux oraenv Tips
Linux for the Oracle DBA -Customizing the Oracle User's Environment There are many ways to customize ...
- java 字符串截取的几种方式
1.split()+正则表达式来进行截取. 将正则传入split().返回的是一个字符串数组类型.不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时. String str = " ...
- Android开发学习--MVP模式入门
1.模型与视图完全分离,我们可以修改视图而不影响模型2.可以更高效地使用模型,因为所有的交互都发生在一个地方——Presenter内部3.我们可以将一个Presenter用于多个视图,而不需要改变Pr ...