The problem here , requires me to find the best possible combination of values in an array which is nearest to 0 and are adjacent to each other , here is an little excerpt from it,
The government of Siruseri has just commissioned one of the longest and most modern railway routes in the world. This route runs the entire length of Siruseri and passes through many of the big cities and a large number of small towns and villages in Siruseri.
The railway stations along this route have all been constructed keeping in mind the comfort of the travellers. Every station has big parking lots, comfortable waiting rooms and plenty of space for eateries. The railway authorities would like to contract out the catering services of these eateries.
The Siruseri Economic Survey has done a through feasibility study of the different stations and documented the expected profits (or losses) for the eateries in all the railway stations on this route. The authorities would like to ensure that every station is catered to. To prevent caterers from bidding only for profitable stations, the authorities have decided to give out catering contracts for contiguous segments of stations.
The minister in charge realises that one of the bidders is his bitter adversary and he has decided to hand out as useless a segment as possible to him. On the other hand, he does not want to be seen to be blatantly unfair by handing out a large loss-making section to the adversary. Instead he wants to find the largest segment whose sum is closest to 0, so that his adversary spends all his time running a large number of canteens and makes either a small loss or a small profit or, even better, nothing at all!
In other words, if the profits/losses at the stations are p1, p2, ..., pN the minister would like to handover a sequence i, i+1, ..., j such that the absolute value of pi + pi+1 + ... + pj is minimized. If there is more than one sequence with this minimum absolute value then he would like to hand over the longest one.
For example, suppose there are 8 stations along the line and their profitability is as follows:
Station 1 2 3 4 5 6 7 8
Expected Profits -20 90 -30 -20 80 -70 -60 125If the adversary is awarded the section 1 through 4, he will make a net profit of 20. On the other hand if he is given stations 6, 7 and 8, he will make loss of 5 rupees. This is the best possible value.
My first approach was to make an 2d array and calculate the sums by , first adding up the number adjacent to current index the use that to add with the next adjacent number and calculate the whole matrix which eventually can give me the number nearest to 0 and their position , but due to huge number of stations , the code gave runtime errors.
Hence changed to a simple solution where you dont have to save each and every result and also break the loop when the output is the minimum possible value that is 0.
The code ran fine with a couple of time limit exceeded cases , but the most astonishing fact is it landed up with 4 wrong answers , the TLE's were expected but wrong answers? I then tested against the test cases , yes couple of them takes a lot of time about 5-6s , but no wrong answers (the set of vertices were different but the question says I can print any set of vertices given the output is minimum) , so probably its a bug in the server.
Here is my code:
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
int main(){
int n;
std::ios_base::sync_with_stdio(false);
std::cin>>n;
std::vector<int>profit(n);
for(int i=0;i<n;i++){
std::cin >> profit[i];
}
int min = INT_MAX ;
int sVertex,eVertex;
for(int i=1;i<n;i++){
int prevCost = profit[i] + profit[i-1];
if(std::abs(prevCost) < std::abs(min)){
min = prevCost;
sVertex = i;
eVertex = i;
}
for(int j=i+1;j<n;j++){
int prof = prevCost + profit[j];
if(std::abs(prof) < std::abs(min)){
min = prof;
sVertex = i;
eVertex = j;
if(min == 0){
std::cout << min << std::endl;
std::cout << sVertex << " " << eVertex+1 << std::endl;
return 0;
}
}
prevCost = prof;
}
}
std::cout << min << std::endl;
std::cout << sVertex << " " << eVertex+1 << std::endl;
return 0;
}
Anyone having a better approach for this problem?
Here are the testcases.