admin管理员组文章数量:1558102
题目链接:http://codeforces/problemset/problem/865/D
题意:You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
给你n个点,每个点可以买,可以卖(前提你有东西),问你最后获利是多少。
思想:直接贪心就行,只要最小的比你小,你就加上这一部分,然后将2倍的放进来。考虑当前卖掉,如果没有比这个数大,那就是卖掉了,如果有的话,那么插入的2个数的第一个数就是一个过渡,直接求差值扔掉就行。
#include<bits/stdc++.h>
using namespace std;
multiset<int>M;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
long long ans=0;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
if(!M.empty() && *M.begin()<temp)
{
ans=ans+(long long)(temp-*M.begin());
M.erase(M.begin());
M.insert(temp);
M.insert(temp);
}
else
M.insert(temp);
}
cout<<ans<<endl;
return 0;
}
本文标签: 贪心codeforcesBuyHighSell
版权声明:本文标题:Codeforces 867 D. Buy Low Sell High (贪心) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727341683a1109492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论