Sweep line algorithm

closed account (1vf9z8AR)
https://www.hackerearth.com/practice/data-structures/trees/heapspriority-queues/practice-problems/algorithm/theatre-830bdbff/description/

My approach is giving tle. How to use sweep line algo in this question? or is there some other technique?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include<iostream>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,k;
    int allmin=1000000000,curmax=-1;
    cin>>n>>k;
    int lim=3*n;
    int arr[lim];
    for(int i=0;i<lim;i++)
        cin>>arr[i];
    int start=0;
    for(int t=0;t<k;t++)
    {
        start=0;
        curmax=-1;
        while(start<lim)
        {
            int curans=(arr[start]+t)%k+(arr[start+1]+t)%k+(arr[start+2]+t)%k;
            if(curans>curmax)
                curmax=curans;
            start+=3;
        }
        if(curmax<allmin)
            allmin=curmax;
    }
    cout<<allmin<<endl;
}
> or is there some other technique?
Yes, you use the clues in the question to figure out a better approach.

"Tag(s): Ad-Hoc, Data Structures, Heap, Heaps/Priority Queues, Medium-Hard, Trees"

> My approach is giving tle.
Well yes, stupid brute-force "try every combination" approaches will almost always result in time limit exceeded because that's a dumb way to solve a problem. Anyone can solve these problems if they're allowed to just post the first dumb answer that comes to mind.

The whole point of the exercise is to use those advanced data structures to quickly prune away whole sets of data from being tested.

Before you even open your IDE and start typing #include<iostream> you need to have solved this problem on paper.


3 6
1 4 2
3 5 0
3 3 5

Step 1, solve this on paper by running your algorithm by hand.
At some point, you'll either
- get bored and start to think about the problem a bit more.
- suddenly realise there is a pattern you can exploit to make a better algorithm.




closed account (1vf9z8AR)
I am still getting tle but passed 5 cases. Any hints on how to optimize it further?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int>plus;
    int n,k;
    cin>>n>>k;
    int a[n],b[n],c[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i]>>c[i];
        plus.insert(k-a[i]);
        plus.insert(k-b[i]);
        plus.insert(k-c[i]);
    }
    int ans=1000000000,curmax=-1,tempsum;
    for(auto x:plus)
    {
        curmax=-1;
        for(int i=0;i<n;i++)
        {
            tempsum=(a[i]+x)%k+(b[i]+x)%k+(c[i]+x)%k;
            if(tempsum>curmax)
                curmax=tempsum;
        }
        if(curmax<ans)
            ans=curmax;
    }
    cout<<ans;
}
> "Tag(s): Ad-Hoc, Data Structures, Heap, Heaps/Priority Queues, Medium-Hard, Trees"
...
> #include<set>
Do you see set mentioned in the hints?
closed account (1vf9z8AR)
I saw data structures written
Topic archived. No new replies allowed.