SRM 600 DIV2

250pt

The company Manao Inc. cares for its employees and tries to provide them with as much comfort as possible. One of the services Manao Inc. provides is transportation of employees from N distant locations to the office. The locations are numbered from 0 to N-1. You are given a vector <int> cnt containing N elements. The i-th (0-based index) element of cnt is the number of employees who live near location i.

The company wants to purchase several shuttles of the same size. These shuttles will then be used for employee transportation. Each of the shuttles will only serve one particular location. Each location will be assigned the smallest number of shuttles sufficient to transport all of the employees living close to it.

The cost of a shuttle consists of the cost baseCost of its frame and some additional cost seatCost per each seat. That is, a shuttle with X seats will cost baseCost + X * seatCost. For its shuttles, Manao Inc. can choose X to be any positive integer. (But remember that all the shuttles must have the same X.) Compute and return the least amount of money the company needs to spend on the shuttles in order to provide transportation for all employees.

解法:

因为元素的数量少,而且范围在1-100之间,所以直接可以用暴力遍历的方法来解。


# include <iostream>
# include <vector>

using namespace std;

class TheShuttles
{
public:
	int getLeastCost(vector <int> cnt, int baseCost, int seatCost)
	{
		int sum,i,j,min;
		int size = cnt.size();
		min = 100000000;
		for(i = 1;i < 101;i ++)
		{
			sum = 0;
			for(j = 0;j < size;j ++)
			{
				int num = 0;
				num = cnt[j]/i;
				if(cnt[j]%i != 0) num += 1;
				sum += (i*seatCost+baseCost) * num;
			}
			if(sum < min) min = sum;
		}
		return min;
	}
};

600pt题目


This problem statement contains subscripts that may not display properly if viewed outside of the applet.

Manao is playing a solitaire game called OR-solitaire. In this game, the player starts with a number X = 0 and should obtain the number goal in one or more moves. The set of valid moves is determined by a vector <int> numbers. In each move, the player chooses some element of numbers and replaces X with the bitwise OR of X and the chosen element.

Fox Ciel wants Manao to stop playing OR-solitaire and move on with his life. She decided to erase some of the elements from numbers in such a way that it becomes impossible to complete the game. Return the minimum number of elements that need to be removed to achieve this

解法:

关于位运算的一个题目。

首先,比目标goal数大的忽略,然后与goal相等的,那么需要除去,然后判断比goal小的,如果如果goal的某位上是0,而元素在改位上是1,那么这类的忽略即可。

最后遍历一下,每个对应goal为1的位上,num数组中的个数,找出最小的即为所求。

# include <iostream>
# include <vector>

using namespace std;

class ORSolitaireDiv2
{
public:
	int getMinimum(vector <int> numbers, int goal)
	{
		int size;
		int arr[100],num[100];
		int i,j,a,b,flag,len;
		int ans;

		size = numbers.size();
		a = goal;
		for(j = 0;j < 100;j ++)
			arr[j] = num[j] = 0;

		for(i = 0;;i++)
		{
			arr[i] = a&1;
			a >>= 1;
			if(a == 0)break;
		}
		ans = 0;
		len = i;
		for(j = 0;j < size;j ++)
		{
			a = numbers[j];
			if(a < goal)
			{
				flag = 1;
				for(i = 0;;i ++)
				{
					if(a&1)
					{
						if(arr[i] == 0)
						{
							flag = 0;
							break;
						}
					}
					a >>= 1;
					if(a == 0)break;
				}
				if(flag)
				
				{
					a = numbers[j];
					for(i = 0;;i ++)
					{
						if(a&1)
						{
							num[i] += 1;
						}
						a >>= 1;
						if(a == 0)break;
					}
				}
			}
			else if(a == goal)
				ans += 1;
		}
		flag = 1;
		for(i = 0;i <= len;i ++)
		{
			if(arr[i] == 1)
			{
				if(num[i] == 0)
				{
					flag = 0;
					break;
				}
			}
		}
		if(flag == 0)
			return ans;
		j = 10000000;
		for(i = 0;i <= len;i ++)
		{
			if(num[i] != 0 && num[i] < j)
				j = num[i];
		}
		return j + ans;
	}
};



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值