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
Limits
Notes
Constraints
Examples
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.
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.)
#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;
}
};