SRM 512 div2 512point

Problem Statement

 A mysterious new restaurant is open in the city for only N days. Happy to hear that, Ash and Elsh would like to have lunch at the restaurant on as many days as possible.

The restaurant sells M types of dishes. Being a mysterious restaurant, it has mysterious rules for the customers:

  1. They can only buy one single dish per day.
  2. If they buy a dish of type j on the i-th day, then on the same day next week, i.e., on the (i+7)-th day, they can only buy a dish of type j.
  3. If they don't buy any dishes on any day, then they can't buy any dishes again from the restaurant.

Mysteriously, the price of each type of dish varies every day. You are given a vector <string> prices consisting of N elements, each containing M characters.prices[i][j] represents the price of the j-th type of dish on the i-th day, encoded as follows:
  • '0' - '9': denotes the price of 0 - 9 dollars.
  • 'A' - 'Z': denotes the price of 10 - 35 dollars.
  • 'a' - 'z': denotes the price of 36 - 61 dollars.

Ash and Elsh have only budget dollars allocated for having lunch in the restaurant. Return the maximum number of days they could have lunch in the restaurant.

Definition

 
Class:MysteriousRestaurant
Method:maxDays
Parameters:vector <string>, int
Returns:int
Method signature:int maxDays(vector <string> prices, int budget)
(be sure your method is public)
 
 

Constraints

-prices will contain between 1 and 50 elements, inclusive.
-Each element of prices will contain the same number of characters, between 1 and 50 characters, inclusive.
-Each character in prices will be '0'-'9', 'a'-'z', or 'A'-'Z'.
-budget will be between 0 and 10,000, inclusive.

Examples

0) 
 
{"26", "14", "72", "39", "32", "85", "06"}
13
Returns: 5
The restaurant is open for 7 days. They can have lunch for 5 days, each picking the cheaper dish from the two available types. The total prices would be 2+1+2+3+2 = 10.
1) 
 
{"26", "14", "72", "39", "32", "85", "06", "91"}
20
Returns: 8
In this case, it is better to buy the second type of dish on the first and the eighth day, so they can have lunch for the entire 8 days.
2) 
 
{"SRM", "512"}
4
Returns: 0
They don't have sufficient budget.
3) 
 
{"Dear", "Code", "rsHa", "veFu", "nInT", "heCh", "alle", "ngeP", "hase", "andb", "ecar", "eful"}
256
Returns: 10


这题是div2中档题,div1的水题,我用了一种错误的算法,看了楼爷的code,才想明白自己wa哪里

ps:终于习惯了tc的输入输出!找到的插件貌似不会测试代码,只是简单的编辑功能,可能我不会用吧~

ac代码:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;


#define sz(a) int((a).size())

class MysteriousRestaurant {
public:
	int maxDays(vector <string>, int);
	int cal(char c);
};

int MysteriousRestaurant::maxDays(vector <string> prices, int budget) {

	int n=prices.size();
	int m=prices[0].size();
	int b=budget;
	int i,j,k,s;
	for(s=n;s>0;s--){        //从后向前查找,妙!
        int a[7][60];
        memset(a,0,sizeof(a));
        for(i=0;i<s;i++)
            for(j=0;j<m;j++)
                a[i%7][j]+=cal(prices[i][j]);
        int sum=0;
        for(i=0;i<7;i++)
        {
            int p=a[i][0];
            for(j=0;j<m;j++)
                p=min(p,a[i][j]);
            sum+=p;
        }
        if(sum<=b)
        return s;
    }
    return 0;
}

int MysteriousRestaurant::cal(char c){
		if(c>='0' && c<='9')
		return c-'0';
		if(c>='A' && c<='Z')
		return c-'A'+10;
		if(c>='a' && c<='z')
		return c-'a'+36;
		return -1;
}

wa代码:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class MysteriousRestaurant {
public:
	int cal(char c)
	{
		if(c>='0' && c<='9')
		return c-'0';
		if(c>='A' && c<='Z')
		return c-'A'+10;
		if(c>='a' && c<='z')
		return c-'a'+36;
	}
	int maxDays(vector<string>prices,int budget){
		
		int choice[100]={0};
		int i,j,k;
		for(i=0;i<prices.size();i++){
			int mn=999999999,idx=0;
			for(j=0;j<prices[0].size();j++){
				int sum=0;
				for(k=i;k<prices.size();k+=7){
					sum+=cal(prices[k][j]);//
					}
					if(sum<mn)
					mn=sum,idx=j;
			}
			choice[i]=idx;
		}
		for(i=0;i<prices.size();i++){
			if(budget < cal(prices[i][choice[i%7]]))
			return i;
			budget-=cal(prices[i][choice[i%7]]);
		}
		return prices.size();
		}
};//错误数据:{"97","97","97","97","97","97","97","19","19","19","19","19","19","19"} 15


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值