Algorithm


Problem link- https://www.spoj.com/problems/HAYBALE/

HAYBALE - Haybale stacking

 

Feeling sorry for all the mischief she has caused around the farm recently, Bessie has agreed to help Farmer John stack up an incoming shipment of hay bales.

She starts with N (1 <= N <= 1,000,000, N odd) empty stacks, numbered 1..N. FJ then gives her a sequence of K instructions (1 <= K <= 25,000), each of the form "A B", meaning that Bessie should add one new haybale to the top of each stack in the range A..B. For example, if Bessie is told "10 13", then she should add a haybale to each of the stacks 10, 11, 12, and 13.

After Bessie finishes stacking haybales according to his instructions, FJ would like to know the median height of his N stacks -- that is, the height of the middle stack if the stacks were to be arranged in sorted order (conveniently, N is odd, so this stack is unique). Please help Bessie determine the answer to FJ's question.

Input

  • Line 1: Two space-separated integers, N K.
  • Lines 2..1+K: Each line contains one of FJ's instructions in the form of two space-separated integers A B (1 <= A <= B <= N).

Output

  • Line 1: The median height of a stack after Bessie completes the instructions.

Sample

Input:
7 4
5 5
2 4
4 6
3 5

Output:
1

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming

N, K = map(int, raw_input().split())
s = [0] * (N + 1)
for i in range(K):
    l, r = map(int, raw_input().split())
    s[l] += 1
    if r != N:
        s[r + 1] -= 1
for i in range(1, N + 1):
    s[i] += s[i - 1]
s = s[0:1] + sorted(s[1:])
print(s[N // 2 + 1])
Copy The Code & Try With Live Editor

Input

x
+
cmd
7 4
5 5
2 4
4 6
3 5

Output

x
+
cmd
1

#2 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 1e6+5;
int arr[MAXN];

int main(){
	io;
	int n, k;
	cin >> n >> k;
	for(int i = 1;i <= k; i++){
		int a, b;
		cin >> a >> b;
		arr[a] += 1;
		arr[b+1] -= 1;
	}
	for(int i = 1;i <= 1000001; i++){
		arr[i] += arr[i-1];
	}
	vector<int> table;
	for(int i = 1;i <= n; i++)
		table.push_back(arr[i]);
	sort(table.begin(), table.end());
	cout << table[n/2] << endl;
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
7 4
5 5
2 4
4 6
3 5

Output

x
+
cmd
1
Advertisements

Demonstration


SPOJ Solution-Haybale stacking-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python