1 / 2
Dec 2019

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

    Dec '19
  • last reply

    Dec '19
  • 1

    reply

  • 599

    views

  • 2

    users

I tried to run your code, but it gave compilation errors. It would help me help you if I could run the code without first having to figure out how to make it compile

Main.java:10: error: Reader is abstract; cannot be instantiated
Reader br = new Reader();

So, please add these lines to the bottom of your loop (i.e. after you write the answer) and run the test cases. What do they give? Are the debug for the first and third cases different?

 int totalQueue = pqBot.size() + pqTop.size();
 bw.write(totalQueue + "\n");

3
1 2 3 10
1 2 3 100
1 2 3 10