Goodland Electricity
Problem Statement :
Goodland is a country with a number of evenly spaced cities along a line. The distance between adjacent cities is unit. There is an energy infrastructure project planning meeting, and the government needs to know the fewest number of power plants needed to provide electricity to the entire list of cities. Determine that number. If it cannot be done, return -1. You are given a list of city data. Cities that may contain a power plant have been labeled . Others not suitable for building a plant are labeled . Given a distribution range of , find the lowest number of plants that must be built such that all cities are served. The distribution range limits supply to cities where distance is less than k. Function Description Complete the pylons function in the editor below. pylons has the following parameter(s): int k: the distribution range int arr[n]: each city's suitability as a building site Returns int: the minimum number of plants required or -1 Input Format The first line contains two space-separated integers and , the number of cities in Goodland and the plants' range constant. The second line contains space-separated binary integers where each integer indicates suitability for building a plant. Output Format Print a single integer denoting the minimum number of plants that must be built so that all of Goodland's cities have electricity. If this is not possible for the given value of k , print -1.
Solution :
Solution in C :
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,k,s,i,j,a,f,z=0;
scanf("%d %d",&n,&k);
int arr[n];
int brr[n];
for(i=0;i<n;i++){scanf("%d",&arr[i]);brr[i]=0;}
s=0;a=0;f=0;
while(a<n)
{
// printf("%d\n",a);
if(a+k-1>n-1&&f==0){a=n-k;f=1;}
for(i=a+k-1;i>=0;i--){
z++;
if(z>100000){printf("-1\n");return 0;}
if(arr[i]==2){printf("-1\n");return 0;}
if(arr[i]==1){
for(j=0;j<k;j++){
if(i+j<n)brr[i+j]=1;
if(i-j>=0)brr[i-j]=1;
}
arr[i]=2;a=i+k;break;}
}
}
for(i=0;i<n;i++){if(brr[i]!=1){printf("-1\n");return 0;}}
s=0;
for(i=0;i<n;i++){if(arr[i]==2){s++;}}
printf("%d\n",s);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Solution in C++ :
In C++ :
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
int main() {
int n; int k;
while(~scanf("%d%d", &n, &k)) {
vector<int> a(n);
for(int i = 0; i < n; ++ i)
scanf("%d", &a[i]);
vi v;
rep(i, n) if(a[i] != 0)
v.push_back(i);
int j = 0;
int ans = 0;
for(int i = 0; i < n; ) {
for(; j + 1 < (int)v.size() && v[j + 1] <= i + k - 1; ++ j);
if(j == v.size() || i + k - 1 < v[j]) {
ans = INF;
break;
}
++ ans;
i = v[j] + k;
++ j;
}
printf("%d\n", ans == INF ? -1 : ans);
}
return 0;
}
Solution in Java :
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] towers = new int[n];
for (int i = 0; i < n; i++) {
towers[i] = sc.nextInt();
}
int i = 0;
int maxTower = k-1;
int ans = 0;
while (true) {
int nextTower = -1;
while (i <= maxTower && i < n) {
if (towers[i] == 1) {
nextTower = i;
}
i++;
}
if (nextTower == -1) {
System.out.println(-1);
return;
}
ans++;
i = nextTower + 1;
maxTower = nextTower+2*k-1;
if (nextTower+k>=n)
break;
}
System.out.println(ans);
}
}
Solution in Python :
In Python3 :
n, k = map(int, input().split())
tower = list(map(int, input().split())) # 0 - no tower; 1 - tower
def voisins(i):
return range(min(n-1, i+k-1), max(-1, i-k), -1)
count = 0
past_fourni = 0
while past_fourni < n:
for i in voisins(past_fourni):
if tower[i]:
past_fourni = i + k
count += 1
break
else:
count = -1
break
print(count)
View More Similar Problems
Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →Balanced Forest
Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a
View Solution →Jenny's Subtrees
Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .
View Solution →Tree Coordinates
We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For
View Solution →Array Pairs
Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .
View Solution →