第三届河南省程序设计大赛 ——AMAZING AUCTION

题目描述

Recently the auction house has introduced a new type of auction, the lowest price auction. In this new system, people compete for the lowest bid price, as opposed to what they did in the past. What an amazing thing! Now you could buy cool stuff with one penny. Your task is to write the software to automate this auction system. 
First the auctioneer puts an upper limit on bid price for each item. Only positive price less than or equal to this price limit is a valid bid. For example, if the price limit is 100, then 1 to 100, inclusive, are all valid bid prices. Bidder can not put more than one bid for the same price on a same item. However they can put many bids on a same item, as long as the prices are different. 
After all bids are set, the auctioneer chooses the winner according to the following rules: 
(1). If any valid price comes from only one bidder, the price is a "unique bid". If there are unique bids, then the unique bid with the lowest price wins. This price is the winning price and the only bidder is the winning bidder. 
(2). If there are no unique bids, then the price with fewest bids is the winning bid. If there are more than one price which has the same lowest bid count, choose the lowest one. This price is the winning price. The bidder who puts this bid first is the winning bidder. 
Given the price limit and all the bids that happen in order, you will determine the winning bidder and the winning price.

输入

The first line contains two integers: U (1 <= U <= 1000), the price upper limit and M (1 <= M <= 100), the total number of bids. M lines follow, each of which presents a single bid. The bid contains the bidder's name (consecutive non-whitespace characters<=5) and the price P (1 <= P <= U), separated with a single space. All bids in the input are guaranteed to be valid ones.

输出

Print the sentence "The winner is W" on the first line, and "The price is P" on the second.

样例输入 

<span style="color:#333333"><span style="color:#333333">30 7
Mary 10
Mary 20
Mary 30
Bob 10
Bob 30
Carl 30
Alice 23
</span></span>

样例输出 

<span style="color:#333333"><span style="color:#333333">The winner is Mary
The price is 20</span></span>

 

题意:

1.出价不高于u的出价为有效出价

2.出价次数最少且出价最低且最先出价的一方获胜

(例如:李出价1 2 3 。刘出价1 3 4 。张出价1 2 3 4。 

则此时李依靠出价2获胜。因为1和3都是有三个人出价,2和4是两个人出价。又因2低于4,所以出价2的人获胜。又因李比张先出价,所以李获胜)

这一题也算是比较水的题,最重要的步骤是排序, 

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
struct price{  //用来盛放输入的投标 
	string s;   //投标人 
	int p;      //投标价格 
	int num1;   //此投标价格出现的次数 
};
bool cmp(struct price p1,struct price p2){    //排序规则为出现次数少的排序在前,若次数相等,则价格低的在前 
	return p1.num1<p2.num1||p1.num1==p2.num1&&p1.p<p2.p;
}
int main()
{
	int u,m;
	while(scanf("%d%d",&u,&m)!=EOF){
		int book[1001]={0};    //由于价格不超过1000,所以用数组盛放不同价格出现的次数 
	    struct price pr[m+1];   
	    int p,i=0;
		string s;
	    for(int j=0;j<m;j++){   //有m个输入 
		    cin>>s>>p;
		    if(p<=u){   //过滤掉不符合要求即大于u的投标 
		    	pr[i].s=s;
		    	pr[i].p=p;
		    	book[pr[i].p]++;   //此价格出现次数加一 
		    	i++;
		    }
	    }
	    m=i;     //更新m为有效投标量 
	    for(int i=0;i<m;i++){    
	    	pr[i].num1=book[pr[i].p];    //每一个投标的相同价格出现的次数 
	    } 
	    sort(pr,pr+m,cmp);     //排序 ,排序后最靠前的为中标 
		cout<<"The winner is "<<pr[0].s<<endl;
		cout<<"The price is "<<pr[0].p<<endl;
	}
	return 0; 
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值