TOPCODER_SRM580_DIV2_2---------枚举

Problem Statement

 Rabbit went to a river to catch eels. All eels are currently swimming down the stream at the same speed. Rabbit is standing by the river, downstream from all the eels.

Each point on the river has a coordinate. The coordinates increase as we go down the stream. Initially, Rabbit is standing at the origin, and all eels have non-positive coordinates.

You are given two vector <int>s: l and t. These describe the current configuration of eels. The speed of each eel is 1 (one). For each i, the length of eel number i is l[i]. The head of eel number i will arrive at the coordinate 0 precisely at the time t[i]. Therefore, at any time T the eel number i has its head at the coordinate T-t[i], and its tail at the coordinate T-t[i]-l[i].

Rabbit may only catch an eel when some part of the eel (between head and tail, inclusive) is at the same coordinate as the rabbit. Rabbit can catch eels at most twice. Each time he decides to catch eels, he may catch as many of the currently available eels as he wants. (That is, he can only catch eels that are in front of him at that instant, and he is allowed and able to catch multiple eels at once.)

Return the maximal total number of eels Rabbit can catch.

Definition

 
Class:EelAndRabbit
Method:getmax
Parameters:vector <int>, vector <int>
Returns:int
Method signature:int getmax(vector <int> l, vector <int> t)
(be sure your method is public)
 
 

Constraints

-l will contain between 1 and 50 elements, inclusive.
-Each element of l will be between 1 and 1,000,000,000, inclusive.
-l and t will contain the same number of elements.
-Each element of t will be between 0 and 1,000,000,000, inclusive.

Examples

0) 
 
{2, 4, 3, 2, 2, 1, 10}
{2, 6, 3, 7, 0, 2, 0}
Returns: 6
Rabbit can catch 6 eels in the following way:
  • At time 2, catch Eel 0, Eel 4, Eel 5, and Eel 6.
  • At time 8, catch Eel 1 and Eel 3.
1) 
 
{1, 1, 1}
{2, 0, 4}
Returns: 2
No two eels are in front of Rabbit at the same time, so Rabbit can catch at most two eels.
2) 
 
{1}
{1}
Returns: 1
3) 
 
{8, 2, 1, 10, 8, 6, 3, 1, 2, 5}
{17, 27, 26, 11, 1, 27, 23, 12, 11, 13}
Returns: 7

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


题目意思:

有n个eels,告诉你们他的头,和长度,即t[i],和l[i]

然后要去抓捕他们,捕杀的人站在坐标为0的地方

然后这些鱼是会随着时间移动,速度一样,并且会经过0

只有当eel的身体的一部分位于0的时候才可以捕杀到

并且一次可以捕杀多个,然后最多可以捕杀2次

我们应该可以马上意识到,即使时间会变,但是整个的相对位置是不会变的,所以就不用管时间什么的了

可以把每个eel看作一条线段,那么如果线段有重叠部分的话,就表明可以一次捕杀

所以我们的目标就是怎么找出在两次内可以穿过最多的线段

解法就是:

我们可以任意枚举两个eel的头部,即表明我们的两次捕杀的位置是在这两个头部位于0时

那么对于其它的eel的话,只要身子和这两个头部有重叠的话,就可以捕杀到

然后找出最大的就OK啦

下面上代码:

#include<vector>
using namespace std;

class EelAndRabbit
{
    public:

    int getmax(vector<int> l,vector<int> t)
    {
        int len=l.size();
        if(len<=2)
            return len;
        int res=0;
        for(int i=0;i<len;i++)
        {
            for(int j=i+1;j<len;j++)
            {
                int ta=t[i];
                int tb=t[j];
                int tmp=0;
                for(int k=0;k<len;k++)
                {
                    if((t[k]<=ta && t[k]+l[k]>=ta) || (t[k]<=tb&&t[k]+l[k]>=tb))
                        tmp++;
                }
                if(tmp>res)
                    res=tmp;
            }
        }
        return res;
    }
};





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值