SRM803_DIV2_450Points

Problem Area

Problem Statement

One of the challenges that comes with a marriage ceremony is charging phones. Our fortunate bride is always busy, people always call her and her phone always needs to be recharged. In this problem you will have to help her.


The bride is about to enter the wedding forum. The wedding forum is circular. Along its circumference there is a walkway. The walkway is exactly forumLength meters long.

We will introduce a coordinate system for the walkway. One designated spot will have the coordinate 0. All other points on the walkway will have increasing coordinates as you go around the forum clockwise. For example, if your current coordinate is forumLength-2 and you start walking in clockwise direction, after two meters you will be back at coordinate 0.


There are N dear relatives of the bride sitting around the forum. The relatives are numbered from 0 to N-1 in the order in which they are sitting. Relative number i is located at the coordinate relative[i].

Each relative wants to greet the bride and talk to her for some amount of time. The bride can talk to the relative when she is at the relative's coordinate. As long as they talk, the bride can also charge her cell phone. The greeting with relative i can last for at most greeting[i] seconds. Charging the cellphone for X seconds gives it enough charge to survive for X more meters of the bride's walk.


The bride will enter the forum with a completely empty battery in her cell phone. Once she enters, she is expected to walk one complete loop around the forum in clockwise direction, eventually returning back to the spot where she started. During the loop she may stop to greet relatives and recharge her cell phone as many times as she wants. Can she do it in such a way that her phone always has some charge while she's walking?

The bride can choose any entrance she wants to enter the forum. More precisely, she can start her walk at any coordinate. Count all locations from which it is possible to complete the loop in such a way that she never walks with a cell phone that is completely out of charge. Return the number of such locations.

Definition

Class:
MarriageAndChargingChallenge
Method:
solve
Parameters:
int, vector <int>, vector <int>
Returns:
int
Method signature:
int solve(int forumLength, vector <int> relative, vector <int> greeting)
(be sure your method is public)

Limits

Time limit (s):
2.000
Memory limit (MB):
256

Notes

- The value N is not given explicitly. You can determine it by looking at the length of relative.
- There is no upper bound on the amount of charge in the bride's cell phone: it has a battery with a sufficiently large capacity.

Constraints

- forumLength will be between 1 and 10^9, inclusive.
- relative will contain between 1 and 50 elements, inclusive.
- Each element of relative will be between 0 and forumLength-1, inclusive.
- Elements of relative will be in increasing order.
- greeting will contain the same number of elements as relative.
- Each element of greeting will be positive.
- The sum of all elements of greeting won't exceed 10^9.

Examples

0)
1000
{100, 300, 500, 700, 900}
{200, 200, 200, 200, 200}
Returns: 5

The walk has 1000 meters. There are five relatives. They sit at coordinates 100, 300, 500, 700, and 900. The bride can talk to each relative for up to 200 seconds. If she does so, she will recharge her phone just enough to last for the walk to the next relative.

Clearly, the bride must start her walk at the coordinate of one of her relatives (otherwise she would have a dead cell phone immediately). She can pick any of these five, in each case we will be able to complete the loop.

1)
1000
{0, 42}
{1234567, 47}
Returns: 1

The bride can start at location 0. If she talks to the relative there for at least 1000 seconds, she will charge her phone enough to complete the full circuit without having to stop again.

The best the bride can to if she starts at location 42 is to talk to the relative there for 47 seconds. Then she will walk for 47 meters and at that point (at coordinate 89) her phone will die. (Note that the bride is not allowed to walk in the opposite direction: she cannot go backwards from location 42 to location 0.)

2)
1000000000
{123456, 7890123, 456789012}
{222222222, 333333333, 444444444}
Returns: 0
Regardless of where the bride starts, she will not be able to make a complete loop. (If she starts at 123456, she will almost be able to do it: her phone will die one meter before she completes the loop.)
3)
1
{0}
{1}
Returns: 1
The smallest possible test: the circuit has just one meter. The bride starts at the only relative here. She will charge her phone for one second and then walk for one meter to complete the loop.
4)
1000000
{42, 47}
{6, 999994}
Returns: 1
Intuitively you may expect that the bride should always start at a relative that is willing to talk to her for a long time, so that she can recharge her phone for a long time. However, that's not always the optimal strategy. Here, starting at location 42 works but starting at location 47 does not.

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.
 
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>

using namespace std;

class MarriageAndChargingChallenge {
    public:
    int solve(int forumLength, vector<int> relative, vector<int> greeting) {
       int n = 0;
        int size = relative.size();
        n = size;
        int greeting1;
        int relative1;
        vector<int> index;
        for (int i = 0; i < size; i++)
        {
            greeting1 = relative[i] + greeting[i];
            relative1 = relative[i];
            for (int j = i + 1; j < size; j++)
            {
                index.push_back(j);
            }
            for (int j = 0; j <= i; j++)
            {
                index.push_back(j);
            }
            for (int k = 0; k < index.size(); k++)
            {
                if (relative1 < relative[index[k]])
                {
                    if (greeting1 < relative[index[k]])
                    {
                        n--;
                        break;
                    }
                    else
                    {
                        greeting1 += greeting[index[k]];
                    }
                    relative1 = relative[index[k]];
                }
                else
                {
                    if(greeting1 < (forumLength+relative[index[k]]))
                    {
                        n--;
                        break;
                    }
                    else{
                        greeting1 += greeting[index[k]];
                    }
                    relative1 = relative[index[k]];
                }
            }
            index.clear();
        }
        return n;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值