cf#317-B-Order Book-水题

cf#317-B-Order Book-水题

给你 n个数据,和一个S

一件物品 属性有D、P,V是数量
格式:Di Pi  Vi
如果Di一样并且Pi一样,要把Vi合并  (v是数量)
最后 输出前S个属性D为“sell”的物品(价格越小越前)         按照Pi降序输出
 输出前S个属性D为“buy”的物品(价格越大越前)       按照Pi降序输出





#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;


struct node
{
	int p;
	int v;
	char d;
};
node tm[1005];
int cmp(node a,node b)
{
	if (a.d!=b.d)
		return a.d>b.d;
	else
		if (a.p!=b.p)
			return a.p<b.p;
		else
			return a.v<b.v;
		
}
int cmp3(node a,node b)
{ 
	return a.p>b.p; 
}


node ans[1005];
int main()
{
	int i,j,n,s;
	scanf("%d%d",&n,&s);
	getchar();
	for (i=1;i<=n;i++)
	{
		scanf("%c %d %d",&tm[i].d,&tm[i].p,&tm[i].v);
		getchar();
	}
	
	sort(tm+1,tm+1+n,cmp);
	int last=1;
	for (i=2;i<=n;i++)
	{
		if (tm[i].d==tm[last].d&&tm[i].p==tm[last].p)
		{
			tm[last].v+=tm[i].v;
			tm[i].d='X';
		}
		else
			last=i;
	}
	int cun=1;
	int ok=0;
	for (i=1;cun<=s&&i<=n;i++)
	{
		if (tm[i].d=='X') continue;
		if (tm[i].d=='B') break; 
		cun++;
		ans[++ok]=tm[i]; //取出前S个sell物品
	} 
	sort(ans+1,ans+1+ok,cmp3); //对sell物品按Pi排序
	
	for (i=1;i<=ok;i++)
		printf("%c %d %d\n",ans[i].d,ans[i].p,ans[i].v);


	cun=1;
	for (j=n;j>=1&&cun<=s;j--)
	{
		if (tm[j].d=='S') break; 
		if (tm[j].d=='X') continue;
		cun++;
		printf("%c %d %d\n",tm[j].d,tm[j].p,tm[j].v);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	return 0;
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值