cf1455 D. Sequence and Swaps(dp, 思维)

题目:

Problem - 1455D - Codeforces

time limit per test

1.5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are given a sequence aa consisting of nn integers a1,a2,…,ana1,a2,…,an, and an integer xx. Your task is to make the sequence aa sorted (it is considered sorted if the condition a1≤a2≤a3≤⋯≤ana1≤a2≤a3≤⋯≤an holds).

To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer ii such that 1≤i≤n1≤i≤n and ai>xai>x, and swap the values of aiai and xx.

For example, if a=[0,2,3,5,4]a=[0,2,3,5,4], x=1x=1, the following sequence of operations is possible:

  1. choose i=2i=2 (it is possible since a2>xa2>x), then a=[0,1,3,5,4]a=[0,1,3,5,4], x=2x=2;
  2. choose i=3i=3 (it is possible since a3>xa3>x), then a=[0,1,2,5,4]a=[0,1,2,5,4], x=3x=3;
  3. choose i=4i=4 (it is possible since a4>xa4>x), then a=[0,1,2,3,4]a=[0,1,2,3,4], x=5x=5.

Calculate the minimum number of operations you have to perform so that aa becomes sorted, or report that it is impossible.

Input

The first line contains one integer tt (1≤t≤5001≤t≤500) — the number of test cases.

Each test case consists of two lines. The first line contains two integers nn and xx (1≤n≤5001≤n≤500, 0≤x≤5000≤x≤500) — the number of elements in the sequence and the initial value of xx.

The second line contains nn integers a1a1, a2a2, ..., anan (0≤ai≤5000≤ai≤500).

The sum of values of nn over all test cases in the input does not exceed 500500.

Output

For each test case, print one integer — the minimum number of operations you have to perform to make aa sorted, or −1−1, if it is impossible.

Example

input

Copy

6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324

output

Copy

3
0
0
-1
1
3

题意:给定一个数x, 可以序列中替换一个比他大的数,使整个序列变成递增序列。

解题思路:可以先从前往后扫一遍,当前面那个数比当前这个数大的话说明要交换,而最少的交换次数应该是序列中刚好比x大的位置开始。值得注意的是,如果当前的数前面有两个比他大的数是无法做到递增序列的,因为无论替换前面至少有一个数始终比当前这个数大,还有,替换到最后的数必须是前一个数,即a[i - 1], 如果不是说明前面至少有一个数和a[i - 1]一样大。(没注意这个wa了好几次),当然其实只用一个变量记录也行,毕竟这个转移只和前面那个变量有关。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>

using namespace std;

#define ll long long
const int N = 530;
const int mod = 1e9+7;

int n, m, k, t;
int a[N], b[N];
int dp[N];

void solve(){
	int n, x;
	cin >> n >> x;

	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	memset(dp, 0, sizeof dp);
	int pos = 0; a[0] = x;
	for (int i = 2; i <= n; i ++){
		dp[i] = dp[i - 1];
		if (a[i] < a[i - 1]){
			for (int j = pos + 1;  j < i; j ++)
				if (a[pos] < a[j]) {
					if (a[pos] > a[i]) {
						puts("-1");
						return;
					}
					pos = j, dp[i]++;
				}
			
			if (pos != i - 1) {
				puts("-1");
				return;
			}
		}
	}
	printf("%d\n", dp[n]);
}

int main(){
	cin >> t;
	while (t --) solve();
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值