CodeForces-Absolute Sorting

题目

拿到这道题 我首先分析了什么时候不存在,然后通过画山峰图发现如果存在中间高两边低或者中间低两边高这种数据是不行的,于是开始写,结果发现最后一个样例打破了我的猜测,我当时想可能首尾不算,但是写了这么多题,直觉告诉我 我的猜测有问题 与于是开始想朴素的写法

81409090 73215 用计算器算这两个数的中间坐标 73215一直打成73125 导致一直出错 后面还是找到了正确做法
总的来说 这题还是挺好的 使我的大脑旋转

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int range = 2e5 + 10;
int n;
int a[range];
void solve() {
	cin >> n;
	int xmaxn = 1e9;
	int xmini = -1e9;
	for (int i = 1; i <= n + 5; i++)a[i] = 0;
	for (int i = 1; i <= n; i++)cin >> a[i];
	int x;
	if (a[1] < a[2]) {
		int ww = floor(((a[1] + a[1 + 1]) * 1.0) / 2);
		x = ww;
		xmaxn=x;
	} else if (a[1] > a[2]) {
		int ww = (a[1] + a[1 + 1] + 2 - 1) / 2;
		x = ww;
		xmini=x;
	} else x = 1e9;
	for (int i = 2; i <= n - 1 ; i++) {
		if (a[i] < a[i + 1]) {
			//3 4
			int w = floor(((a[i] + a[i + 1]) * 1.0) / 2);
			if (xmini > w) {
				cout << -1 << endl;
				return;
			}
			if(xmaxn!=1e9)	
				xmaxn = min(xmaxn,w);
			else xmaxn=w;
		}
		else if (a[i] == a[i + 1])continue;
		else {
			int w = (a[i] + a[i + 1] + 2 - 1) / 2;
			if (w > xmaxn ) {
				cout << -1 << endl;
				return;
			}	
			//	cout<<w<<endl;
			if(xmini!=-1e9)	
				xmini = max(xmini,w);
			else xmini=w;
		}
	}
	if(xmini<=xmaxn){
		if(xmini==xmaxn)cout<<xmaxn<<endl;
		else if(xmini==-1e9)
		{
			cout<<xmaxn-1<<endl;
			
		}
		else if(xmaxn==1e9)cout<<xmini+1<<endl;
		else cout<<xmini+1<<endl;
	}
	else cout<<-1<<endl;
}
signed main() {
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	for (int i = 1; i <= t; i++)
		solve();
	return 0;
}
posted @ 2025-04-16 19:57  LteShuai  阅读(18)  评论(0)    收藏  举报