1月 20, 2009

【解題】Request for Proposal

@
ACM Volume CI 10141 - Request for Proposal


The Problem

When government, military, or commercial agencies wish to make a major purchase, they first issue a Request for Proposal (RFP) which lists a number of requirements that must be met by a successful proposal. Competing suppliers issue Proposals, indicating which of the requirements are met, and a price that will be charged should the proposal be accepted by the agency issuing the RFP.

Because the agencies are staffed by bureaucrats and are accountable to other agencies staffed by bureaucrats, it is necessary to remove all human judgement from the selection process. To this end, those evaluating the proposals are given feature sheets, which have one column for each requirement and and additional column for price, and one row for each Proposal. The evaluator reads each proposal and identifies each requirement that is met; for each such requirement a check mark is placed in the corresponding row (for the Proposal) and column (for the requirement). After all proposals have been evaluated, the number of check marks in each row is added. Any proposal that has the same number of check marks as the number of requirements is said to be compliant; otherwise the proposal is said to be partially compliant. Many agencies award the contract to the lowest compliant proposal; that is the compliant proposal with the lowest price. If there is no compliant proposal, many agencies evaluate partial compliance according to the following formula:

compliance = number_of_requirements_met / number_of_requirements

Your job is to select the Proposal with the highest compliance; if several proposals have the same compliance you are to select from these proposals the one with the lowest price. If several proposals have the same compliance and price you are to select the first one in the input.


Input

Your input will consist of the information for a number of RFPs and associated proposals. The information for each RFP will consist of:

  • a line containing two integers: 0 < n <= 1000, the number of requirements, and p the number of proposals. The line 0 0 indicates there are no more RFPs.
  • n lines naming the requirements. Each requirement is a string up to 80 characters long, terminated by the end of line. All strings are case sensitive.
  • for each of the p proposals:
    • a line naming the proposal (up to 80 characters terminated by end of line)
    • a line containing a floating point number d and an integer 0 <= r <= n: d is the price; r is the number of met requirement lines to follow.
    • for each met requirement, the name of the requirement, each on a separate line. All requirements are from the RFP requirement list, and no requirements are duplicated.


Output

For each RFP, give the number of the RFP (see sample) followed by the name of the best proposal, optimizing the criteria given above. Leave a blank line between the output for each pair of RFPs.


Sample Input

6 4
engine
brakes
tires
ashtray
vinyl roof
trip computer
Chevrolet
20000.00 3
engine
tires
brakes
Cadillac
70000.00 4
ashtray
vinyl roof
trip computer
engine
Hyundai
10000.00 3
engine
tires
ashtray
Lada
6000.00 1
tires
1 1
coffee
Starbucks
1.50 1
coffee
0 0


Sample Output

RFP #1
Cadillac

RFP #2
Starbucks


解題思考

  這題沒什麼技巧,照著題目的意思來即可:

  首先,挑選廠商的第一條件是符合採購需求的數量。若是有兩家廠商符合需求的數量相同,則挑選兩家廠商中價錢較低的那個。若是兩家廠商的價錢又相同,則挑選較前面的廠商。


參考解答(C++)

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    string str;
    int rep = 1;
    while (1)
    {
        int n, p;
        cin >> n >> p;

        if (!n && !p) { break; }

        if (rep > 1) { cout << endl; }

        // 讀入需求項目
        cin.get();
        for (int i = 0; i < n; i++)
        {
            getline(cin, str);
        }

        string best_name;
        double best_price;
        int best_meet;
        for (int i = 0; i < p; i++)
        {
            // 讀入廠商名稱
            getline(cin, str);

            // 讀入報價及符合需求數
            double price;
            int meet;
            cin >> price >> meet;

            // 根據需求選擇廠商
            if (!i || meet > best_meet ||
                (meet == best_meet && price < best_price))
            {
                best_name = str;
                best_price = price;
                best_meet = meet;
            }

            // 讀入廠商符合需求的項目
            cin.get();
            for (int j = 0; j < meet; j++)
            {
                getline(cin, str);
            }
        }

        cout << "RFP #" << rep++ << endl;
        cout << best_name << endl;
    }

#ifndef ONLINE_JUDGE
    system("pause");
#endif
}

7 回覆:

tom 提到...

my code is not working , but output answer is correct. Can you help me take a look ?

#include
#include
using namespace std;

int main ()
{
int itemNumber,companyNumber;
string * name=NULL;
double * price=NULL;
int * item=NULL;
string record;

int n =0;
while( cin>>itemNumber>>companyNumber)
{
if(itemNumber==0 && companyNumber ==0)
{
break;
}

if(n>0)
{
cout<>price[i]>>item[i];


if(i >0 && (item[i] > item[i-1] || (item[i]== item[i-1] && price[i]<price[i-1])))
{
record=name[i] ;
}
else
{
record=name[0] ;
}

cin.get();
for(int j=0; j<item[i]; j++)
{
getline (cin,temp);
}
}

cout<<"RFP #"<<++n<<endl;
cout<<record<<endl;
}



//system("pause");

return 0;
}

匿名 提到...

i got the iostream and string at the top, i think google blog can not display it. And I tried to submit it on online judge. They just keep saying wrong answer

Unknown 提到...

Hi, I tried to compile your code and got some syntax errors. Such as undeclared variables 'i' and 'temp' and this line: "cout<>price[i]>>item[i];".

I think you should fix your program first and paste your code at CodePad.

tom 提到...

Sorry I don't know why I posted something else in the last post.

my code: http://codepad.org/YCXaXDBP
The point is I use the dynamic arrays, and the ouput is correct. But online judge said wrong answer. I hope you can help me figure it out ! thanks!!!

Unknown 提到...

You should to change the 'if' condition at line 50.

Because the aim of this program is to find the best proposal with the highest compliance (if several proposals have the same compliance, choose the first one with the lowest price), you need to compare the number of requirements met (or the price of the proposal) of the current proposal with the current best one, rather than the previous one.

匿名 提到...

omg!! what a big mistakes!! thanks men!!!!
are u still doing acm exercises?

Unknown 提到...

You're welcome :)
But I haven't done those exercises recently.

張貼留言