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 ...
随机推荐
- Ubuntu 18.04 关闭蓝牙开机启动
sudo gedit /etc/rc.local 然后,加入下面一行 rfkill block bluetooth
- jQuery笔记之事件绑定
.on(),off(),.one(),.trigger() .hover() jQuery实例方法-动画 .show(),.hide(),.toggle() 参数:null或(duration,eas ...
- 题解报告:hdu 1062 Text Reverse
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 Problem Description Ignatius likes to write word ...
- 创建一个长度是5的数组,并填充随机数。使用for循环或者while循环,对这个数组实现反转效果
package day01; import java.util.Random; /** * 首先创建一个长度是5的数组,并填充随机数.使用for循环或者while循环,对这个数组实现反转效果 * @a ...
- 1、IO概述及File类
- 安装CentOS--设置网络_2
(1)虽然CentOS 7已经可以联网,但是在日常的运维工作中,我们是需要手动给Linux系统设置IP地址的.输入如下命令. # vi /etc/sysconfig/network-scripts/i ...
- SpringMvc如何将Url 映射到 RequestMapping (二)
昨天简单分析了Springmvc 中 RequestMapping 配置的url和请求url之间的匹配规则.今天详细的跟踪一下一个请求url如何映射到Controller的对应方法上 一.入口 org ...
- 洛谷——P2680 运输计划
https://www.luogu.org/problem/show?pid=2680 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每 ...
- RequireJS 上手使用
首先 点击此处 得到requirejs. 捣鼓了俩小时终于运行成功了,原因是因为require(['我是空格underscore',...],function(){...})的时候 变量多个空格(坑爹 ...
- 解决vue项目eslint校验 Do not use 'new' for side effects 的两种方法
import Vue from 'vue' import App from './App.vue' import router from './router' new Vue({ el: '#app' ...