SRM 149 1A2AB 2013.12.3
DIV1
250
Problem Statement
BigBurger Inc. wants to see if having asingle person at the counter both to take orders and to serve them is feasible.At each BigBurger, customers will arrive and get in line. When they get to thehead of the line they will place their order, which will be assembled andserved to them. Then they will leave the BigBurger and the next person in linewill be able to order.
We need to know how long a customer may beforced to wait before he or she can place an order. Given a script that listseach customer for a typical day, we want to calculate the maximum customerwaiting time. Each customer in the script is characterized by an arrival time(measured in minutes after the store opened) and a service duration (the numberof minutes between ordering and getting the food).
Create a class BigBurger that containsmethod maxWait that is given a vector <int> arrival and a vector<int> service describing all the customers and returns the maximum timespent by a customer between arriving and placing the order. Correspondingelements of arrival and service refer to the same customer, and they are givenin the order in which they arrive at the store (arrival is in non-descendingorder).
If multiple customers arrive at the sametime they will all join the line at the same time, with the ones listed earlierahead of ones appearing later in the list.
Definition
Class:
BigBurger
Method:
maxWait
Parameters:
vector <int>, vector <int>
Returns:
int
Method signature:
int maxWait(vector <int> arrival,vector <int> service)
(be sure your method is public)
Constraints
-
arrival will contain between 1 and 50elements inclusive
-
service will contain the same number ofelements as arrival
-
the elements of arrival will be innon-decreasing order
-
each element of arrival will be between 1and 720 inclusive
-
each element of service will be between 1and 15 inclusive
Examples
0)
{3,3,9}
{2,15,14}
Returns: 11
Two customers arrive at time 3. The firstone waits 0 time, orders, and is served after 2 minutes, leaving at time 5. Thesecond one then orders and is served at time 20. Meanwhile a customer arrivesat time 9 and waits until the second customer leaves. This last customer thenorders at time 20, and is served and leaves at time 20+14 = 34. The firstcustomer waited 0 minutes, the second waited 2 minutes (from time 3 to time 5),and the last customer waited 11 minutes (from time 9 to time 20).
1)
{182}
{11}
Returns: 0
The first customer never needs to wait.
2)
{2,10,11}
{3,4,3}
Returns: 3
The third customer needs to wait from time11 to time 14. Neither of the other customers needs to wait at all.
3)
{2,10,12}
{15,1,15}
Returns: 7
The second customer waits the longest.
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. is strictlyprohibited. (c)2003, TopCoder, Inc. All rights reserved.
199.44
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class BigBurger
{
public:int maxWait(vector <int>arrival, vector <int> service)
{
int ans=0;
int last=arrival[0]+service[0];
for(int i=1;i<arrival.size();i++)
{
int temp=last-arrival[i];
if (temp<0) temp=0;
ans+=temp;
last=arrival[i]+service[i];
}
return ans;
}
};
DIV2
250
Problem Statement
In documents, it is frequently necessary towrite monetary amounts in a standard format. We have decided to format amountsas follows:
the amount must start with '$'
the amount should have a leading '0' if andonly if it is less then 1 dollar.
the amount must end with a decimal pointand exactly 2 following digits.
the digits to the left of the decimal pointmust be separated into groups of three by commas (a group of one or two digitsmay appear on the left).
Create a class FormatAmt that contains amethod amount that takes two int's, dollars and cents, as inputs and returnsthe properly formatted string.
Definition
Class:
FormatAmt
Method:
amount
Parameters:
int, int
Returns:
string
Method signature:
string amount(int dollars, int cents)
(be sure your method is public)
Notes
-
One dollar is equal to 100 cents.
Constraints
-
dollars will be between 0 and 2,000,000,000inclusive
-
cents will be between 0 and 99 inclusive
Examples
0)
123456
0
Returns: "$123,456.00"
Note that there is no space between the $and the first digit.
1)
49734321
9
Returns: "$49,734,321.09"
2)
0
99
Returns: "$0.99"
Note the leading 0.
3)
249
30
Returns: "$249.30"
4)
1000
1
Returns: "$1,000.01"
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
191.48
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
class FormatAmt
{
public:string amount(int dollars, intcents)
{
stringstream ans,sdollars,scents;
ans<<"$";
sdollars<<dollars;
if (cents<10)scents<<"0";
scents<<cents;
if (dollars==0) ans<<"0";
else
{
string d = sdollars.str();
int len=d.size();
int key=len%3;
if (key!=0)
{
for(int j=0;j<key;j++)
ans<<d[j];
ans<<",";
}
int count=0;
for(int i=key;i<len-1;i++)
{
count++;
ans<<d[i];
if (count%3==0) ans<<",";
}
ans<<d[len-1];
}
ans<<".";
ans<<scents.str();
return ans.str();
}
};
500 同DIV 1 A