public static void main (String[] args) throws java.lang.Exception
{
Reader br = new Reader();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = br.nextInt();
PriorityQueue pqTop;
PriorityQueue pqBot;
for(int t = 0; t < T; t++) {
pqTop = new PriorityQueue<>(Collections.reverseOrder());
pqBot = new PriorityQueue<>();
a = br.nextInt();
b = br.nextInt();
c = br.nextInt();
int n = br.nextInt();
pqBot.add(1);
long sum = 1;
for(int i=2; i<=n; i++) {
if(i%2 == 0) {
pqTop.add(pqBot.remove());
}
int ff = getF(pqTop.peek(), i);
sum += (long)ff;
pqBot.add(ff);
if(pqBot.size() > 0 && pqTop.size() > 0 && pqTop.peek() < pqBot.peek()) {
pqTop.add(pqBot.remove());
pqBot.add(pqTop.remove());
}
}
bw.write(sum + "\n");
}
bw.flush();
bw.close();
}
I am using two priority queue method with fast reading but still getting tle.With same approach a similar question is solved on codechef RRATING. If you got some idea to optimize it please tell.
created
last reply
- 1
reply
- 599
views
- 2
users