https://www.spoj.com/problems/HOTELS/
http://xoptutorials.com/index.php/2017/01/01/spojhotels/
There are N hotels along the beautiful Adriatic coast. Each hotel has its value in Euros.
Sroljo has won M Euros on the lottery. Now he wants to buy a sequence of consecutive hotels, such that the sum of the values of these consecutive hotels is as great as possible - but not greater than M.
You are to calculate this greatest possible total value.
Input
In the first line of the input there are integers N and M (1 ≤ N ≤ 300 000, 1 ≤ M < 231).
In the next line there are N natural numbers less than 106, representing the hotel values in the order they lie along the coast.
Output
Print the required number (it will be greater than 0 in all of the test data).
Example
input5 12 2 1 3 4 5output 12 | input4 9 7 3 5 6output 8 |
Here is an O(n) algorithm for you. Let us keep 3 variables P , Q and S. P and Q will show us an interval which we are considering at the moment and S will show the sum of the current interval. Initially P and Q are 1 and S is A[1]. Suppose he variable which shows the maximum purchase is called M. So now, if currently our S is smaller than M which means that there is still a chance that we might buy the next one as well, we increment Q by 1 meaning that we will be considering now the hotels between P and Q+1. If S is more then M which means that by taking the latest hotel we passed the limit so we increment P by 1 meaning that we are not taking the leftmost hotel. And also don’t forget to update the answer in any step.
int main() { unsigned int p,q,i,j,n,m,b,c,d; unsigned int ans,s; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%d",&a[i]); p=1; q=1; s=a[1]; ans=0; while(q<=n) { if(s<=m) { ans=max(ans,s); q++; s=s+a[q]; } else { p++; s=s-a[p-1]; } } printf("%d",ans); return 0; }https://tp-iiita.quora.com/The-Two-Pointer-Algorithm
Motivation Problem: Given an array having integers, you need to find out a subsequence of integers such that these integers have the minimum hustle. Hustle of a sequence is defined as sum of pair-wise absolute differences divided by the number of pairs. For details on the statement, refer the problem link here
Constraints: Both and K are less than or equal to and each element has absolute value of around .
Anyway, lets see what the problem demands this time. The number of pairs in the function wont be of much significance since we will always be dividing by fixed number of pairs. So, we can ignore the denominator here and the way function "hustle" is defined surely tells us that the numerator of the function will be minimum as much as those integers will be close to each other. Since, hustle function is nothing but merely summation of absolute difference of pairs. Why not just sort the numbers and consider each consecutive contiguous subarray of length for this? That's it. You are done for good !
If you still doubt how we have reduced the problem from subsequence of length to contiguous substring of length , try to contradict yourself by removing one element from contiguous elements and taking some other element, you will surely realize, what wrong happened their and how much the absolute difference increased overall. Try to prove this way to give yourself complete satisfaction about the greedy technique used over here.
Overall complexity of the solution is
- lli A[MAX],C[MAX];
- int main()
- {
- int l = 1, r = 2, st,en,n;
- lli sum,ans;
- cin >> n >> k;
- for ( int i = 1; i <= n; i++ ) cin >> A[i];
- sort(A+1, A+n+1);
- cum[0] = 0;
- for ( int i = 1; i <= n; i++ ) cum[i] = cum[i-1] + A[i];
- while ( r <= k ) {
- sum += (A[r]*(r-l) - (cum[r-1] - cum[l-1]));
- r++;
- }
- st = 1, en = k, ans = sum;
- while ( r <= n ) {
- sum -= (cum[r-1] - cum[l] - A[l]*(r-l-1));
- l++;
- sum += (A[r]*(r-l) - (cum[r-1] - cum[l-1]));
- if ( ans > sum ) {
- ans = sum;
- st = l;
- en = r;
- }
- r++;
- }
- return 0;
- }