Auction

Problem A: 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.

Input Description

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.

The first line of each test case contains two integers: U (1 <= U <= 10,000), the price upper limit and M (1 <= M <= 100,000), 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) and the price P (1 <= P <= U), separated with a single space. All bids in the input are guaranteed to be valid ones.

Output Description

 

For each test case, print the sentence "The winner is W." on the first line, and "The price is P." on the second. Replace W and P with the winning bidder’s name and the winning price.

Sample Input

2

3 3
Alice 1
Bob 2
Carl 3g 

3 6
Alice 1
Alice 2
Alice 3
Bob 1
Bob 3
Carl 3

Sample Output

Case 1:
The winner is Alice.
The price is 1.

Case 2:
The winner is Alice.
The price is 2.
  

 

      题目如上,想了一下,写了这样的一个算法:

#include  < iostream >
#include 
< string >
#include 
< vector >
#include 
< algorithm >
#include 
< list >
using   namespace  std;
struct  bid_info
{
 
string  name;
 
int   value;
 
int  order;
 
bool   operator   == ( const   struct  bid_info &  bi)  const  {
  
if (value  ==  bi.value)  return   true ;
  
return   false ;
 }
};

typedef vector
< struct  bid_info >   bid_list;

bool  comp_bid( const   struct  bid_info  & b1 ,  const   struct  bid_info  &  b2 )
{
 
if (b1.value  ==  b2.value)
  
return  b1.order  ==  b2.order;
 
return  b1.value  <  b2.value;
}
void  output( int  i , bid_list::iterator it)
{
 cout 
<< " Case  "   <<  i  << " : "   << endl;
 cout 
<< " The winner is  "   <<  it -> name  << " . "   <<  endl;
 cout 
<< " The price is  "   <<  it -> value  << " . "   << endl;
}
void  find_winner( int  i, bid_list  &  list)
{
 bid_list::iterator first 
=  list.begin();
 
while (first != list.end())
 {
  
if (count(list.begin(),list.end(), * first)  ==   1 ) {
   output(i,first);
   
return  ;
  } 
else  first ++ ;
 }
 output(i,list.begin());
}

void  print( struct  bid_info  &  bidinfo)
{
 cout 
<< bidinfo.name  << "   "   <<  bidinfo.value  << endl;
}
int  main()
{
 
int  i,n ,j;
 
int  up, m ;
 bid_list  list;
 
struct  bid_info   bidinfo;

 cin 
>>  n ;
 
for (i  =   0  ; i  <  n ; i  ++  )
 {
  cin 
>>  up  >>  m ;
  list.erase(list.begin(),list.end());
  
for (j  =   0  ; j  <  m ; j  ++ )
  {
   cin 
>>  bidinfo.name;
   cin 
>>  bidinfo.value;
   
if (bidinfo.value  <=  up  &&  bidinfo.value  >   0  )
    list.push_back(bidinfo);
  }
  stable_sort(list.begin(),list.end(), comp_bid); 
  
// for_each(list.begin(),list.end(),print);
  cout  << endl;
  find_winner(i
+ 1 ,list);
 }
 
return   0 ;
}

 

        这个算法耗时的地方主要在sort 和 find_winner 中对于每个元素进行遍历。 find_winner 的时间复杂度应该是O(n^2)  。 根据评测获胜者的规则以及输入的特殊性,可以改进如下:

#include  < iostream >
#include 
< string >

using   namespace  std;
#define  NMAX 10000
struct  bid_info
{
 
string  name;
 
int   count;
};
void  output( int  i ,  int  value,  string  name)
{
 cout 
<< endl;
 cout 
<< " Case  "   <<  i  <<   " : "   << endl;
 cout 
<< " The winner is  "   <<  name  << " . "   << endl ;
 cout 
<< " The price is  "   <<  value  << " . "   << endl;
}
int  main()
{
 
struct  bid_info bids[NMAX];
 
int  i,j;
 
int  up, n,m ;
 
int  value;
 
int  index;
 
string  name;
 
bool  bfirst ;

 cin 
>>  n ;
 
for (i  =   0  ; i  <  n ; i  ++  )
 {
  
for (j  =   0  ; j  <  NMAX ; j  ++ )
   bids[j].count 
=   0  ;
  cin 
>>  up  >>  m ;
  
for (j  =   0  ; j  <  m ; j  ++ )
  {
   cin 
>>  name;
   cin 
>>  value;
   
if (value  <=  up  &&  value  >   0  )
   {
    bids[value].count 
++ ;
    
if (bids[value].count  ==   1 )
     bids[value].name 
=  name;
   }
  }
  bfirst 
=   false ;
  

 
for (j  =   0  ; j  <  NMAX ; j  ++ )
  {
   
if (bids[j].count  ==   0 continue ;
   
else   if (bids[j].count  ==   1 ) {
    bfirst 
=   false ;
    output( i 
+   1  , j , bids[j].name);
    
break ;
   } 
else   if (bfirst  ==   false ) {
    bfirst 
=   true ;
    index 
=  j;
   }
  }
  
if (bfirst  ==   true )
  {
   output(i 
+ 1 , index, bids[index].name);
  }
 }
 
return   0 ;
}

 

   对于每种价格只是保持第一个bid 的信息。通过scan 整个数组可以找出获胜者!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值