Increase Sequence
时间限制:1000 ms | 内存限制:65535 KB
难度:2
-
描述
-
You have an array A with N elements.Your goal is to change it into an array that contains each number from 1 to N exactly once. The change will consist of zero or more steps. In each step, you may pick an arbitrary element of A and increase its value by k. You may pick the same element multiple times. Note that you are not allowed to decrease the value of any element.
-
输入
- There are multiple cases.
For each input case, the first line contains two integers N and k ,the second line contains N intergers A[i]
(i=0,1,2,…,N-1).
(1≤N,A[i]≤50,1≤k≤10)
输出 - Print “POSSIBLE”if you can achieve your goal, print “IMPOSSIBLE” otherwise. 样例输入
-
5 2 1 2 3 3 5 4 3 1 4 3 2
样例输出 -
IMPOSSIBLE POSSIBLE
- There are multiple cases.
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int number = scanner.nextInt();
int value = scanner.nextInt();
int arr[] = new int[number];
boolean flag = true;
for (int i = 0; i < number; i++) {
arr[i] = scanner.nextInt() - 1;
}
Arrays.sort(arr);
for (int i = 0; i < number;) {
if (arr[i] == i) {
i++;
continue;
}
while (arr[i] < i) {
arr[i] += value;
}
Arrays.sort(arr);
if (arr[i] > i) {
flag = false;
break;
}
}
if (flag) {
System.out.println("POSSIBLE");
} else {
System.out.println("IMPOSSIBLE");
}
}
}
}