This Problem is bugging me

Revision en1, by Apophis404, 2025-06-16 23:14:50

There are $$$N$$$ glasses placed in a row (numbered from $$$1$$$ to $$$N$$$). The $$$i$$$-th glass has:

  • a total volume of $$$V_i$$$ liters,
  • currently contains $$$W_i$$$ liters of water.

A glass is called good if

$$$ W_i \; \gt \; \tfrac12\,V_i, $$$

and bad otherwise.


Bending Operation

You may perform up to $$$K$$$ non‑intersecting merge operations. In each operation you select a contiguous segment of glasses $$$[l, r]$$$ (with $$$1 \le l \lt r \le N$$$) and merge them into one new glass whose:

  • volume is
$$$ V = \sum_{i=l}^r V_i, $$$
  • water amount is
$$$ W = \sum_{i=l}^r W_i. $$$

Segments $$$[l_1,r_1]$$$ and $$$[l_2,r_2]$$$ are non‑intersecting if

$$$ \{l_1, l_1+1, \ldots, r_1\}\,\cap\,\{l_2, l_2+1, \ldots, r_2\} = \varnothing. $$$

Goal

Determine whether it is possible—by choosing some $$$K$$$ non‑intersecting segments (where $$$0 \le K \le N$$$) and merging each—to end up with strictly more good glasses than bad glasses.

  • If yes, print:

Yes K l_1 r_1 l_2 r_2 ⋮ l_K r_K

where each $$$[l_i, r_i]$$$ is a chosen segment. * Otherwise, print:

No


Input

  • The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of test cases.
  • Each test case consists of:
  1. An integer $$$N$$$ $$$\bigl(1 \le N \le 10^5\bigr)$$$ — the number of glasses.
  2. A line of $$$N$$$ integers $$$V_1, V_2, \ldots, V_N$$$ $$$\bigl(1 \le V_i \le 10^9\bigr)$$$ — the volume of each glass.
  3. A line of $$$N$$$ integers $$$W_1, W_2, \ldots, W_N$$$ $$$\bigl(0 \le W_i \le V_i \le 10^9\bigr)$$$ — the current water in each glass.

It is guaranteed that $$$\sum N \le 2\times10^5$$$ over all test cases.


Output

For each test case:

  • If no sequence of merges yields strictly more good glasses than bad, print No.
  • Otherwise, print Yes, the integer $$$K$$$, and then $$$K$$$ lines each containing two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 \le l_i \lt r_i \le N)$$$, representing your chosen non‑intersecting segments.

You may output any valid solution.


Example

Input
2
4
6 4 8 10
4 1 6 1
3
4 4 5
1 2 3

Output
Yes
1
2 3
No

My Solution :

https://ideone.com/S8gKLm

The basic idea was :

1) to merge the consecutive negatives and gain something like + — + — ....

2) if #positives == #negatives, we try to merge a + — pair with sum > 0

3) if #negatives — #positives == 1, we try to merge the first + — pair and last + — pair with sum > 0.

I cant understand why this solution doesnt work. And it would be really helpful if someone points out a solution/editorial to this problem as well.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Apophis404 2025-06-16 23:14:50 2746 Initial revision (published)