Increase Sequence

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

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");
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值