T-shirt buying CodeForces - 799B (小根堆+STL)
思路:
由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆,
我是根据pair<int,int> 创建了一个以价格小的优先的优先队列。
pair中的另外一个int,用来存这个衣服的id,即用来标记这个衣服有没有已经被卖了。
详细看代码哦
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct cmp
{
bool operator()(const pii p1, const pii p2)
{
return p1.second > p2.second; //second的小值优先
}
};
// fi -> id se -> prices
priority_queue<pii,vector<pii>,cmp> h[];
int n;
int m;
int num[maxn];
bool bj[maxn];
int main()
{
gg(n);
repd(i,,n)
{
gg(num[i]);
}
int c;
repd(i,,n)
{
gg(c);
h[c].push(mp(i,num[i]));
}
repd(i,,n)
{
gg(c);
h[c].push(mp(i,num[i]));
}
gg(m);
repd(i,,m)
{
gg(c);
if(h[c].empty())
{
printf("-1 ");
}else
{
int flag=;
while(flag&&!h[c].empty())
{
pii tem=h[c].top();
h[c].pop();
if(bj[tem.fi]==)
{
printf("%d ",tem.second);
flag=;
bj[tem.fi]=;
}else
{
continue;
}
}
if(flag)
{ printf("-1 ");
}
} }
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
T-shirt buying CodeForces - 799B (小根堆+STL)的更多相关文章
- CJOJ 2482 【POI2000】促销活动(STL优先队列,大根堆,小根堆)
CJOJ 2482 [POI2000]促销活动(STL优先队列,大根堆,小根堆) Description 促销活动遵守以下规则: 一个消费者 -- 想参加促销活动的消费者,在账单下记下他自己所付的费用 ...
- 【CF799B】T-shirt buying(一道很水的小根堆)
点此看题面 大致题意: 有\(n\)件T恤衫,告诉你每件T恤衫的价格以及它正面和反面的颜色(\(1≤\)颜色的编号\(≤3\)),现在有m个顾客,已知每个人想要的衣服的颜色(一件T恤衫只要有一面的颜色 ...
- 让priority_queue支持小根堆的几种方法
点击这里了解什么是priority_queue 前言 priority_queue默认是大根堆,也就是大的元素会放在前面 例如 #include<iostream> #include< ...
- scala写算法-用小根堆解决topK
topK问题是指从大量数据中获取最大(或最小)的k个数,比如从全校学生中寻找成绩最高的500名学生等等. 本问题可采用小根堆解决.思路是先把源数据中的前k个数放入堆中,然后构建堆,使其保持堆序(可以简 ...
- 堆-STL
往堆中加一个元素的算法(put): #include<algorithm> void put (int d) { heap[++heap_size]=d; push_heap(heap+, ...
- 随手练——HDU Safe Or Unsafe (小根堆解决哈夫曼问题)
HDU 2527 :http://acm.hdu.edu.cn/showproblem.php?pid=2527 哈夫曼树,学完就忘得差不多了,题目的意思都没看懂,有时间复习下,看了别人的才知道是怎么 ...
- 05-树6. Path in a Heap (25) 小根堆
05-树6. Path in a Heap (25) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...
- 优先队列实现 大小根堆 解决top k 问题
摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...
- bzoj 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle——小根堆+大根堆+贪心
Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1& ...
随机推荐
- Android Studio插件GsonFormat
GsonFormat插件用于在androidStudio 根据json自动生成class的字段和方法,极大提高了开发效率 一.安装GsonFormat插件 二.重启Android Studio,新建一 ...
- f.lux 自动调节显示器色温
我的环境 f.lux 我的使用感受是让屏幕看起来舒服一些,因为我有近视,所以需要保护眼睛. f.lux官网:https://justgetflux.com/ f.lux v4.47 windows 1 ...
- no plugin found for prefix 'tomcat 7' in the current project and in the plugin groups的解决方法
解决方法一: 找到这个settings.xml文件,进行编辑,在pluginGroups标签下加入下面的配置 <pluginGroups><pluginGroup>org.ap ...
- Linux 中数组的使用
Linux中数组本人可能用的相对较少,但是会经常遇见,也容易忘记,就顺便记录下来吧 数值类型的数组:一对括号表示数组,数组中元素之间使用“空格”来隔开 arr=(1 2 3 4 5) 字符串类型数组: ...
- wrk 压力测试 http benchmark POST接口
简单的 http 性能测试工具 wrk.git 一个简单的 http benchmark 工具, 能做很多基本的 http 性能测试. wrk 的一个很好的特性就是能用很少的线程压出很大的并发量. 原 ...
- Spark Streaming和Kafka整合保证数据零丢失
当我们正确地部署好Spark Streaming,我们就可以使用Spark Streaming提供的零数据丢失机制.为了体验这个关键的特性,你需要满足以下几个先决条件: 1.输入的数据来自可靠的数据源 ...
- (转)Spring Boot 2 (三):Spring Boot 开源软件都有哪些?
http://www.ityouknow.com/springboot/2018/03/05/spring-boot-open-source.html 2016年 Spring Boot 还没有被广泛 ...
- 2.01-request_header
import urllib.request def load_baidu(): url= "https://www.baidu.com" header = { #浏览器的版本 &q ...
- UVA11400-Lighting System Design(动态规划基础)
Problem UVA11400-Lighting System Design Accept: 654 Submit: 4654Time Limit: 3000 mSec Problem Descr ...
- ELMO模型(Deep contextualized word representation)
1 概述 word embedding 是现在自然语言处理中最常用的 word representation 的方法,常用的word embedding 是word2vec的方法,然而word2vec ...