C1. Skibidus and Fanum Tax (easy version) time lim...
Создано: 9 февраля 2025 г.
Создано: 9 февраля 2025 г.
C1. Skibidus and Fanum Tax (easy version)
time limit per test2 seconds
memory limit per test256 megabytes
This is the easy version of the problem. In this version, 𝑚=1
.
Skibidus has obtained two arrays 𝑎
and 𝑏
, containing 𝑛
and 𝑚
elements respectively. For each integer 𝑖
from 1
to 𝑛
, he is allowed to perform the operation at most once:
Choose an integer 𝑗
such that 1≤𝑗≤𝑚
. Set 𝑎𝑖:=𝑏𝑗−𝑎𝑖
. Note that 𝑎𝑖
may become non-positive as a result of this operation.
Skibidus needs your help determining whether he can sort 𝑎
in non-decreasing order∗
by performing the above operation some number of times.
∗
𝑎
is sorted in non-decreasing order if 𝑎1≤𝑎2≤…≤𝑎𝑛
.
Input
The first line contains an integer 𝑡
(1≤𝑡≤104
) — the number of test cases.
The first line of each test case contains two integers 𝑛
and 𝑚
(1≤𝑛≤2⋅105
, 𝐦 = 1
).
The following line of each test case contains 𝑛
integers 𝑎1,𝑎2,…,𝑎𝑛
(1≤𝑎𝑖≤109
).
The following line of each test case contains 𝑚
integers 𝑏1,𝑏2,…,𝑏𝑚
(1≤𝑏𝑖≤109
).
It is guaranteed that the sum of 𝑛
and the sum of 𝑚
over all test cases does not exceed 2⋅105
.
Output
For each test case, if it is possible to sort 𝑎
in non-decreasing order, print "YES" on a new line. Otherwise, print "NO" on a new line.
You can output the answer in any case. For example, the strings "yEs", "yes", and "Yes" will also be recognized as positive responses.
Example
InputCopy
5
1 1
5
9
3 1
1 4 3
3
4 1
1 4 2 5
6
4 1
5 4 10 5
4
3 1
9 8 7
8
OutputCopy
YES
NO
YES
NO
YES
Note
In the first test case, [5]
is already sorted.
In the second test case, it can be shown that it is impossible.
In the third test case, we can set 𝑎3:=𝑏1−𝑎3=6−2=4
. The sequence [1,4,4,5]
is in nondecreasing order.
In the last case, we can apply operations on each index. The sequence becomes [−1,0,1]
, which is in nondecreasing order.
correct solution in java
To solve this problem, we need to determine whether it's possible to sort the array a in non-decreasing order by applying the given operation. The operation allows us to change an element a[i] by setting it to b[j] - a[i] where b[j] is a value from array b.
a to b1 - a[i] (since m=1, there is only one value b1 in array b).a in non-decreasing order, at each index i, we can either leave a[i] unchanged or modify it to b1 - a[i].a[i] whether to leave it as it is or change it to b1 - a[i] such that the entire array becomes non-decreasing.For each test case:
n, m, arrays a and b. Since m=1, there's only one value in b, b1.a such that after performing the operations, a becomes sorted.a[i], we can either keep it as a[i] or set it to b1 - a[i].i, a[i] or b1 - a[i] is not greater than a[i+1] or b1 - a[i+1] (whichever is chosen for the next element).Algorithm:
a and at each step, check if it's possible to either leave the current element or modify it, maintaining the non-decreasing order.javaimport java.util.Scanner; public class SkibidusAndFanumTax { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); // number of test cases while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); // this will always be 1 int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int b1 = sc.nextInt(); // only one element in b boolean possible = true; // Start from the first element int prev = a[0]; int prevOption = b1 - a[0]; for (int i = 1; i < n; i++) { int current = a[i]; int currentOption = b1 - a[i]; // We want to maintain a non-decreasing order if (Math.min(prev, prevOption) > Math.max(current, currentOption)) { possible = false; break; } // Update the prev values if (Math.max(prev, prevOption) <= current) { prev = current; prevOption = currentOption; } else if (Math.max(prev, prevOption) <= currentOption) { prev = currentOption; prevOption = current; } else { possible = false; break; } } System.out.println(possible ? "YES" : "NO"); } sc.close(); } }
n, m, the array a, and the value b1 from the input.a. For each element, we check if we can maintain a valid non-decreasing sequence by considering both the current element and its possible modified value (i.e., b1 - a[i]).a, so the time complexity for each test case is O(n).n across all test cases is at most 2 * 10^5, the overall time complexity will be O(2 * 10^5), which is efficient and within the problem's constraints.Input:
text5 1 1 5 9 3 1 1 4 3 3 4 1 1 4 2 5 6 4 1 5 4 10 5 4 3 1 9 8 7 8
Output:
textYES NO YES NO YES
This approach efficiently solves the problem and handles the constraints well.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class SkibidusAndFanumTax {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
textwhile (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int[] a = new int[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); } int b1 = Integer.parseInt(br.readLine().trim()); boolean possible = true; int prev = a[0]; int prevOption = b1 - a[0]; for (int i = 1; i < n; i++) { int current = a[i]; int currentOption = b1 - a[i]; if (Math.min(prev, prevOption) > Math.max(current, currentOption)) { possible = false; break; } if (Math.max(prev, prevOption) <= current) { prev = current; prevOption = currentOption; } else if (Math.max(prev, prevOption) <= currentOption) { prev = currentOption; prevOption = current; } else { possible = false; break; } } System.out.println(possible ? "YES" : "NO"); } }
}By *Yaduvanshi_Sarvan, contest: Codeforces Round 1003 (Div. 4), problem: (C1) Skibidus and Fanum Tax (easy version), Wrong answer on test 1, #, Copy→Judgement Protocol
Test: #1, time: 171 ms., memory: 0 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER
Input
5
1 1
5
9
3 1
1 4 3
3
4 1
1 4 2 5
6
4 1
5 4 10 5
4
3 1
9 8 7
8
Output
YES
NO
NO
NO
NO
Checker Log
wrong answer expected YES, found NO [3rd token] fix this error
Analysis
You are given:
For each element , you can either keep it as is or flip it to . The question is whether there exists a choice (keep or flip for each element) that makes the resulting array non-decreasing.
A direct but incorrect approach is to track two "previous" values at each step and compare them with the two possible new values. This can fail, as demonstrated by the sample tests where it incorrectly returns "NO" instead of "YES."
The correct greedy insight is:
Why does always picking the smallest feasible candidate (if it is ) work?
It can be shown via a standard argument for “non-decreasing choice” problems that if there is any valid sequence, this left-to-right “choose the smallest feasible” method will find one.
javaimport java.io.*; import java.util.StringTokenizer; public class SkibidusAndFanumTax { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); while (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); // m=1 in the easy version int[] a = new int[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); } // There's only one element in b: int b1 = Integer.parseInt(br.readLine().trim()); // Step 1: Compute x_0 and y_0 for the first element int x0 = Math.min(a[0], b1 - a[0]); int y0 = Math.max(a[0], b1 - a[0]); // We'll choose c[0] = x0 (the smaller feasible value for the first element) int current = x0; boolean possible = true; // Step 2: For each subsequent element, pick the smallest feasible option for (int i = 1; i < n; i++) { int x = Math.min(a[i], b1 - a[i]); int y = Math.max(a[i], b1 - a[i]); // We need c[i] >= current. So try x first, else y if (x >= current) { current = x; } else if (y >= current) { current = y; } else { possible = false; break; } } // Step 3: Output the result System.out.println(possible ? "YES" : "NO"); } } }
current which represents the chosen value . For the next element :
current = x_0 because if there's a valid sorted sequence starting with , there would also be one starting with the smaller (picking a smaller value first can only help or do no harm in a non-decreasing sequence).This corrected approach ensures we only make one pass ( per test) and correctly handles all edge cases, including the ones where flipping is essential to maintain the sorted order.