【hdu 6438】Buy and Resell
【链接】 我是链接,点我呀:)
【题意】
有一个物品的价格在1..n这些位置各不相同。
你初始有无限的钱。
问你从1走到n.
你每次可以选择买入一个或者卖出一个该种物品(或啥都不做)
问你最后的最大利润。
【题解】
定义一个数据类型为pair temp的优先队列q。
其中second的值为1或者2
**如果为1**
那么说明这个订单代表的是一个"已经配对"的订单。
即代表有序对(x,y)且x
【代码】
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e5;
int n;
int a[N+10];
long long ans,num;
priority_queue <pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
int main()
{
//freopen("D:\\cpp_program\\rush0\\rush.txt","r",stdin);
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while (T--){
ans = 0;num = 0;
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
while (!q.empty()) q.pop();
for (int i = 1;i <= n;i++){
if (!q.empty() && q.top().first<a[i]){
pair<int,int> temp = q.top();q.pop();
ans+=a[i]-temp.first;
if (temp.second==1){
q.push(make_pair(temp.first,2));
}else{
num+=2;
}
q.push(make_pair(a[i],1));
}else q.push(make_pair(a[i],2));
}
cout<<ans<<' '<<num<<endl;
}
return 0;
}
【hdu 6438】Buy and Resell的更多相关文章
- 【HDU 1133】 Buy the Ticket (卡特兰数)
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...
- 【HDU 2126】Buy the souvenirs(01背包)
When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souve ...
- 【noi 2.6_9288】&【hdu 1133】Buy the Ticket(DP / 排列组合 Catalan+高精度除法)
题意:有m个人有一张50元的纸币,n个人有一张100元的纸币.他们要在一个原始存金为0元的售票处买一张50元的票,问一共有几种方案数. 解法:(学习了他人的推导后~) 1.Catalan数的应用7的变 ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
随机推荐
- 【iOS开发系列】九宫格布局
/** * 这个尽管非常easy,算是一个小技巧,可是碰到了就记录下来吧.积跬步,致千里嘛. */ - (void)scratchableLatex { for (int i=0; i<9; i ...
- 【MongoDB】windows平台搭建Mongo数据库复制集(相似集群)(三)
关于windows平台搭建Mongo数据库复制集这个话题,我已经在前面写了两篇博客 第一篇: 怎样在windows平台搭建Mongo数据库复制集 第二篇: 数据同步和故障自适应測试 在本篇里面,咱们重 ...
- 数据结构之---C++语言实现图的十字链表存储表示
近期一直忙着考研复习,非常久都没有更新博客了.今天写一篇数据结构的存储. //有向图的十字链表存储表示 //杨鑫 #include <iostream> #include <cstd ...
- Errors occurred during the build. Errors running builder 'Integrated External Tool Builder' on proje
Errors occurred during the build. Errors running builder 'Integrated External Tool Builder' on proje ...
- makepy
文件连接: https://files.cnblogs.com/files/mophy/%E7%99%BB%E5%BD%95%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90.7 ...
- 利用SQLite在android上实现增删改查
利用SQLite在android上实现增删改查 方法: 一.直接利用database.execSQL()方法输入完整sql语句进行操作 这种方法适用于复杂的sql语句,比如多表查询等等 这里适合于增删 ...
- [Pulgin] jQuery插件之ajaxFileUpload
一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数 ...
- Triangle 1.6 (A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator)
Triangle 一个二维高质量网格(mesh)生成器和Delaunay三角化工具. PSLG(Planar Straight Line Graph)约束Delaunay三角网(CDT)与Delaun ...
- Redis五种数据类型及应用场景
MySql+Memcached架构的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不断增加 ...
- 从Android源码分析View绘制
在开发过程中,我们常常会来自定义View.它是用户交互组件的基本组成部分,负责展示图像和处理事件,通常被当做自定义组件的基类继承.那么今天就通过源码来仔细分析一下View是如何被创建以及在绘制过程中发 ...