nyoj 251 有趣的竞选(STL vector容器)

AMAZING AUCTION

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4

描述

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.

输入
There are multi test cases.EOF will terminate the input.
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.
样例输入
30 7
Mary 10
Mary 20
Mary 30
Bob 10
Bob 30
Carl 30
Alice 23
样例输出
The winner is Mary
The price is 20

题意:

模拟一个拍卖活动,要求你找到拍卖赢家,买家可以对一个物品出多次价格但不能相同,你需要判断的赢家,赢家有两种判断方式

如果有唯一的竞猜价格,找到最小的价格即为该竞猜价格,该人为获奖的人,如果没有唯一的竞猜价格,这找最小的价格输出竞猜次数最少的人:

思路:

用vector容器储存字符串,将相同竞猜价格的人放在一起,,用一个一维数组统计各个竞猜价格的个数,寻找只有一个数的值,寻找到的第一个就是最小的值,如果不存在唯一的值,那就找最小的那个输出!

#include<stdio.h>
#include<vector>
#include<string.h>
#include<string>
#include<iostream>
#define inf 0x3f3f3f3f
using namespace std;
struct point
{
    string n;//竞选者名字
    int m;
} p[1005];
int main()
{
    int an,u,t,i,f[1005],pe;
    vector<string>q[1005];//容器
    while(~scanf("%d%d",&u,&t))
    {
        for(i=0; i<=u; i++)
            q[i].clear();//先把容器清空
        memset(f,0,sizeof(f));
        for(i=0; i<t; i++)
        {
            cin>>p[i].n>>p[i].m;
            q[p[i].m].push_back(p[i].n);//输入容器中
            f[p[i].m]++; //统计每个拍卖价格的数量
        } 
        an=inf;
        string ans;
        int f1=0;
        vector<string>::iterator it;//迭代器
        for(i=0; i<=u; i++)
            if(f[i]==1)  //如果这个数据只出现了一次
            {
                an=i; //最后的拍卖价格就是i
                it=q[i].begin();//
                ans=*it;
                f1=1; //如果存在唯一的最低价格,f1=1
                break;
            }//第一次查找
        if(!f1)  //如果不存在唯一的最低价格,二次查找
            for(i=0; i<=u; i++)
                if(f[i]>1)
                {
                    it=q[i].begin();
                    ans=*it;
                    an=i;  //找到价格最低的
                    break;
                }
        cout<<"The winner is "<<ans<<endl;
        printf("The price is %d\n",an);

    }
return 0;
}


你可能感兴趣的:(nyoj 251 有趣的竞选(STL vector容器))