I don't think your heaps are balanced as zukow suspected. The number of elements in the heaps should differ atmost by 1. And don't use the % operator heavily. The expression :
val = (((a*hleft[0])%mod + (b*(i+1))%mod + c%mod))%mod;
is equal to this :
val = (a*hleft[0] + b * (i + 1) + c) % mod;
if a, b and c are int64 types.
4 months later
5 months later
8 months later
I'm getting TLE for this code. What optimizations are needed to get AC. I'm using max priority queue for the left half and min priority queue for right half. median would be the top of max queue.
#include <iostream>
#include <queue>
using namespace std;
#define MOD 1000000007
typedef unsigned long long lng;
int main()
{
//freopen("in","r",stdin);
int a, b, c, n;
int t;
cin >> t;
for (int I=1; I<= t; I++ )
{
priority_queue<int> maxpq;
priority_queue<int, vector<int>, greater<int> > minpq;
cin >> a >> b >> c >> n;
lng sum = 1LL;
maxpq.push(1);
for (int J=2; J<=n;J++)
{
lng top = lng (maxpq.top());
lng tempsum = ((lng(a)*lng(maxpq.top()))%MOD + (lng(b)*lng(J))%MOD + lng(c))%MOD;
sum += tempsum;
int tmp = int(tempsum);
if (J&1)
{
if (tmp > minpq.top())
{
int move = minpq.top();
minpq.pop();
maxpq.push(move);
minpq.push(tmp);
}
else maxpq.push(tmp);
}
else
{
if (tmp > maxpq.top())
minpq.push(tmp);
else
{
int move = maxpq.top();
maxpq.pop();
minpq.push(move);
maxpq.push(tmp);
}
}
}
cout << sum << endl;
}
return 0;
}
I thought making all calculations lng would be more time consuming than to use type casting in just single line. Is it a wrong assumption?
@leppy: Now I've made all calculations lng but it's still TLE
@leppy: thanks. got AC
changes I've made:
lng tempsum = ((a*maxpq.top()) + (b*J) + c)%MOD;
TLE was because of unnecessary use of MOD.
Suggested Topics
Topic | Category | Replies | Views | Activity |
---|---|---|---|---|
What are allowed characters in task t9 | ProblemSet Archive | 4 | 232 | Feb 24 |
Help me please! | ProblemSet Archive | 4 | 45 | 28d |
ABCPATH - ABC Path | ProblemSet Archive | 1 | 103 | May 11 |
What am i Missing | ProblemSet Archive | 1 | 173 | Feb 22 |
Beangame | ProblemSet Archive | 2 | 138 | Apr 9 |