CCPC2018-A-Buy and Resell
1. spend ai dollars to buy a Power Cube
2. resell a Power Cube and get ai dollars if he has at least one Power Cube
3. do nothing
Obviously, Noswal can own more than one Power Cubes at the same time. After going to the n cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning.
The first line has an integer n. (1≤n≤105)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1≤ai≤109)
It is guaranteed that the sum of all n is no more than 5×105.
4
1 2 10 9
5
9 5 9 10 5
2
2 1
In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16
题意是有n个城市,给出一个商品在这n个城市的价格,商人从第一个城市到第n个城市,在每个城市可以选择买入、卖出或者不操作,可以携带多个商品,问最大收益和最小操作次数 先考虑贪心进行选择,到达一个城市之后,如果之前有城市的价格比他低我就在前面的城市买入在这里卖出
但这样显然是有问题的,比如第一组样例1 ,这样操作的话答案是 2-=,但实际应该是 10-+-(或者10-+-)
我们考虑用优先队列来维护出现过的价格,当我们在某个城市选择以a价格买入b价格卖出时,我们向队列里加入两个b,并且把b-a先加进答案中
那么如果在之后的操作中,我们想以a买入以c卖出,我们的实际操作是买入队列中的b以c卖出,这时我们的收益是c-b+b-a=c-a,而且还有一个b在队列中 至于操作次数,如果是买入加入队列两次的那个物品操作次数不变,否则加二
#include <bits/stdc++.h>
#define ll long long
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
map<int,int>mp;
int T,n,x;
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
ll ans=;
int cnt=;
mp.clear();
for (int i=;i<=n;i++)
{
scanf("%d",&x);
if (!q.empty() && x>q.top())
{
ans+=x-q.top();
if (mp[q.top()]) mp[q.top()]--;
else cnt+=;
q.pop();
q.push(x);
mp[x]++;
}
q.push(x);
}
printf("%lld %d\n",ans,cnt);
while (!q.empty()) q.pop();
}
return ;
}
CCPC2018-A-Buy and Resell的更多相关文章
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心
题目传送门 题目描述: 有n座城市,每座城市都可以对一个物品进行一次的买进或者卖出,可以同时拥有多个物品,计算利润最大值,并且交易次数要最少.(买入卖出算两次操作) 思路: 建立两个小根堆 优先队列, ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu3438 Buy and Resell(优先队列+贪心)
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 2018 CCPC 网络赛 Buy and Resell
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- hdu6438 Buy and Resell
多少年不写题了... (我把每一天看作是一个商品,第i天是第i个商品) 一开始看了半天看出来一个性质:买的所有商品中最贵的不会比卖的所有商品中最便宜的贵,然后似乎没有什么用处.... 所以最后还是看题 ...
- HDU6438:Buy and Resell(贪心+数据结构)
题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少 分析:对每一个元素产生的贡献可以先计算 ...
- HDU6438 Buy and Resell 解题报告(一个有趣的贪心问题的严格证明)
写在前面 此题是一个很容易想到的贪心题目,但是正确性的证明是非常复杂的.然而,目前网上所有题解并未给出本题贪心算法的任何正确性证明,全部仅停留在描述出一个贪心算法.本着对算法与计算机科学的热爱(逃), ...
- 【hdu 6438】Buy and Resell
[链接] 我是链接,点我呀:) [题意] 有一个物品的价格在1..n这些位置各不相同. 你初始有无限的钱. 问你从1走到n. 你每次可以选择买入一个或者卖出一个该种物品(或啥都不做) 问你最后的最大利 ...
随机推荐
- Scrum立会报告+燃尽图(十月十四日总第五次):前期宣传工作进行中
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2195 Scrum立会master:段晓睿 一.小组介绍 组长:付佳 组员 ...
- C++寒假计划
课程 西北工业大学的c++程序设计 理由 这个课程里的内容都比较详细,能比较全面的讲解C++,我们是从C过渡到C++的,所以我之前看了阚道洪的面向对象程序设计的课程,他讲解了两者的差别,还有C++对C ...
- 移动平台的meta标签
这个meta在移动平台上有非常神奇的地方. 1. <meta name="viewport" content="width=device-width; initia ...
- 一次WebSphere性能问题诊断过程
一次接到用户电话,说某个应用在并发量稍大的情况下就会出现响应时间陡然增大,同时管理控制台的响应时间也很慢,几乎无法进行正常工作. 赶到现场后,查看平台版本为Webshpere6.0.2.29,操作系统 ...
- 将 Spring 和 Hibernate 与 WebSphere Application Server 一起使用
本文摘要 如果您考虑将 Spring 或 Hibernate 与 IBM® WebSphere® Application Server 一起使用,则本文向您阐述了如何配置这些框架,以适用于 WebSp ...
- java一些知识
类名前只有两种修饰符:不写(即default,但不能把default写上去)或public.默认不写则此类只能被同一包下的类调用以生成相应的实例.但若是public,则可以被不同包下的类调用以生成其实 ...
- form表单元素中disabled的元素的值不会提交到服务器
1.表单元素中disabled的元素的值不会提交到服务器,后台获取的值为null <form id="myForm" action="#" method= ...
- 复利计算1.0,2.0,3.0(java)
程序源代码: import java.util.Scanner; public class ch { public static void main(String[] args) { Scanner ...
- 【vue】vue安装卡住/报错
网上有很多教程怎么安装: 安装cnpmnpm install -g cnpm --registry=https://registry.npm.taobao.org 升级npmcnpm install ...
- pixi.js tools
pixi群 881784250 Awesome pixi.js tools A list of useful libs/resources/tools for renowned html5 rende ...