夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

SRM153 Div.2 Easy MostProfitable

さて二連続で書いてるんであまりネタありませんが・・・

どーん!

条件分岐は頭を整理してから

問題文はコレ

PROBLEM STATEMENT
When selling goods, it is important to know exactly how 
much it costs to acquire each item.  A number of 
distributed costs, such as marketing often make this 
difficult, but not impossible.  If a business can figure 
out how much an item costs, with some accuracy, then it 
can easily calculate the profit margins for the item.  
This information, combined with sales figures, can be used 
to determine which items are the most important to a 
business.  In this problem you will be given the costs, 
prices, and number of sales for a number of items.  Each 
element of costs represents the total costs accrued from 
selling a single item.  The corresponding elements (ones 
with the same index) of prices and sales represent the 
prices at which single items are sold, and the number of 
sales of each item that have occurred in some time period, 
respectively.  You are to return the name of the item (the 
corresponding element of items) that provides the business 
with the most profits.  If there is a tie for the most 
profitable item, return the one that comes earliest in 
items (lowest index).  If no item provides the business 
with positive profits you should return the empty string.



DEFINITION
Class:MostProfitable
Method:bestItem
Parameters:vector , vector , vector , 
vector 
Returns:string
Method signature:string bestItem(vector  costs, 
vector  prices, vector  sales, vector  
items)


CONSTRAINTS
 -costs, prices, sales, and items will all contain the same 
number of elements.
 -costs, prices, sales, and items will contain between 1 
and 50 elements, inclusive.
 -Each element of costs and prices will be between 1 and 
1,000,000, inclusive.
 -Each element of sales will be between 0 and 1,000, 
inclusive.
 -Each element of items will have between 1 and 50 
characters, inclusive.
 -Each character of each element of items will have ASCII 
value between 32 and 126, inclusive.  (All of the 
characters that can be easily made with a regular keyboard.)

長いようだけども、サンプルとか見てすぐ理解。

条件分岐を最初に間違えてセグメンテーションフォルトとかだしてたけどなんとかうまくいった。

class MostProfitable {
   public:
   string bestItem(vector <int> costs, vector <int> prices, vector <int> sales, vector <string> items)
  {
  	string best="";
  	int score=0, i, at=costs.size(), pr;
	for (i=0; i<costs.size(); i++) {
		pr = (prices[i]-costs[i])*sales[i];
		if (pr <= 0) {
			continue;
		} else if (score < pr) {
			at = i;
			score = pr;
		}
	}
	if (at != costs.size()) best = items[at];
	return best;
  }
};

さて、MMでも考えようかな。。。