题目链接

http://codeforces.com/problemset/problem/799/B

题意

给出N件衣服

pi 表示 第i件衣服的价格

ai 表示 第i件衣服的前面的颜色

bi 表示 第i件衣服的后面的颜色

然后有 m 位顾客

顾客会给出 他中意的颜色 只要 衣服的前面或者后面有这个颜色 那么 这件衣服 就是符合要求的 然后他要购买 目前还剩下的 最便宜的那件

先来先服务原则 并且 每件衣服 只有一件

思路

用 三个 vector 来保存 三个颜色的衣服 卖出 后 要标记一下

刚开始 T 了 因为我们每次都是从 头开始遍历的

后来想想 可以用三个 标记 分别标记 三个 vector 的头 这样 前面已经遍历过的 就可以不用 遍历 (还是 大佬提醒 才做出来)

刚开始 要给这三个 vector sort

对了 这个 价格 应该都是不同的 因为用 sort 都能过

大佬 给普及了一下 sort 的效率 最快是 nlogn 只有当 每个元素都不同时

最慢是 n^2 每个元素都相同时

插入排序是比较稳定的 nlogn

AC代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<set>
#include<list>
#include<vector>
#include<deque>
#include<stack> #define ll long long using namespace std; const int INF=0x3f3f3f3f;
const int maxn=2e5+10; struct node
{
int v, id;
}q[maxn]; vector <node> c[3]; int visit[maxn]; bool comp(node x, node y)
{
return x.v < y.v;
} int main()
{
memset(visit, 0, sizeof(visit));
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &q[i].v);
q[i].id = i;
}
int color;
for (int i = 0; i < n; i++)
{
scanf("%d", &color);
c[color - 1].push_back(q[i]);
}
for (int i = 0; i < n; i++)
{
scanf("%d", &color);
c[color - 1].push_back(q[i]);
}
sort(c[0].begin(), c[0].end(), comp);
sort(c[1].begin(), c[1].end(), comp);
sort(c[2].begin(), c[2].end(), comp);
int m;
scanf("%d", &m);
int count[3] = {0};
int flag[3] = {0};
for (int i = 0; i < m; i++)
{
scanf("%d", &color);
if (i)
printf(" ");
int len = c[color - 1].size();
if (count[color - 1] >= len)
{
printf("-1");
continue;
}
int ans = -1;
for (int j = flag[color - 1]; j < len; j++)
{
if (visit[c[color - 1][j].id] == 0)
{
visit[c[color - 1][j].id] = 1;
ans = c[color - 1][j].v;
count[color - 1]++;
flag[color - 1] = j + 1;
break;
}
}
printf("%d", ans);
}
printf("\n");
}

CodeForces - 799B T-shirt buying 【贪心】的更多相关文章

  1. 【codeforces 799B】T-shirt buying

    [题目链接]:http://codeforces.com/contest/799/problem/B [题意] 告诉你每个人喜欢的衣服的颜色; 然后告诉你每件衣服的正面和背面的颜色以及它的价格; 只要 ...

  2. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  3. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  4. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  5. Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]

    题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...

  6. Codeforces 799B - T-shirt buying(STL)

    题目链接:http://codeforces.com/problemset/problem/799/B 题目大意:有n件T恤,每件T体恤都分别有价格(每件衣服的价格不重复).前面的颜色.背部的颜色三种 ...

  7. T-shirt buying CodeForces - 799B (小根堆+STL)

    题目链接 思路: 由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆, 我是根据pair<int,int> 创建了一个以价格小的优先的优先队列. pair中的另外一个i ...

  8. Codeforces 1136D Nastya Is Buying Lunch (贪心)

    题意: 给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置. 分析: 这题是思路是十分的巧妙呀 , 用一个数组num[x]  ...

  9. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

随机推荐

  1. MySQL开发36条军规

    转载地址:http://blog.itpub.net/22664653/viewspace-723506/ 写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒过 ...

  2. Python数据结构:列表、元组和字典

    在Python中有三种内建的数据结构——列表list.元组tuple和字典dict 列表中的项目包括在方括号中,项目之间用逗号分割 元组和列表十分类似,只不过元组和字符串一样是不可变的 即你不能修改元 ...

  3. web前端的一些实用技能

    如今我们使用的互联网,客户端与服务器端的交互无时无刻不在发生.比如我们在浏览器打开网页,浏览器就是客户端,将网页数据发过来的也就是服务器.其实服务器,并没有什么特别的,也就是一台昼夜不停运转的电脑罢了 ...

  4. vscode 编译调试c/c++的环境配置

    首先看了一下别人写的文章 http://blog.csdn.net/c_duoduo/article/details/51615381 在按照上文链接博主的安装步骤进行到MINGW的安装时出现一个问题 ...

  5. BZOJ 1293 SCOI2009 生日礼物 堆

    题目大意:给定一个数轴上n个点,每一个点有一种颜色,一共k种颜色.求一个最短的区间,包括全部k种颜色 卡了一段时间0.0 一開始想二分答案啥的 后来发现数据范围太大写不了0.0 后来去找题解才发现尼玛 ...

  6. linux 源代码安装mysql5.5

    linux下源代码安装mysql过程例如以下: yum update yum upgrade yum install -y vim man wget yum install -y gcc gcc-c+ ...

  7. Struts2学习二----------访问Servlet API

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2提供了三种方式去访问Servlet API -ActionContext -实现*Aware接口 -ServletActionConte ...

  8. Redis(九):使用RedisTemplate访问Redis数据结构API大全

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  9. freemark2pdf

    freemarker+ITextRenderer 生成html转pdf 博客分类: ITextRenderer ITextRenderer  网上已经有比较多的例子 写这个 但是很多都是简单的 dem ...

  10. leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...