B. Speedbreaker

链接:https://codeforces.com/problemset/problem/2018/B
题目:

思路:
刚开始的思路是对的,就是每个点确定范围求交集。然后还要判断一下会不会无解,比如[5,6,4,1,4,5],就是说∃t∈[1,n],st:a[x]<=n,a[y]<=n,max(x)-min(y)>=t时无解,因为两侧如果更大、更小就会冲突。
代码:

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int N = 2e5 + 10;

int a[N];
struct pa
{
	int first, sec;
	pa(int f, int s) { first = f; sec = s; }
	pa(){}
};
bool cmp_s(pa a, pa b) { return a.sec < b.sec; }
void solve()
{
	int n; cin >> n;
	for (int i = 1; i <= n; i++) { int x; cin >> x; a[i] = x; }
	int l =0 , r = LLONG_MAX;
	for (int i = 1; i <= n; i++)
	{
		int lnow = max((int)1, i - a[i] + 1);
		int rnow = min(n, i + a[i] - 1);
		l = max(l, lnow), r = min(r, rnow);
	}
	vector<pa>vt;
	for (int i = 1; i <= n; i++)vt.push_back(pa(i, a[i]));
	sort(vt.begin(), vt.end(), cmp_s);
	int maxn = 0, minn = LLONG_MAX;
	int cnt = 1;
	bool jmp = false;
	for (pa x : vt)
	{
		if (jmp)break;
		minn = min(minn, x.first);
		maxn = max(maxn, x.first);
		cnt = x.sec;//这里直接修改cnt=x.sec就可以
		if (maxn - minn >= cnt)jmp = true;
		
	}
	if (jmp)cout << 0;
	else cout << r - l + 1;
	cout << '\n';


}


signed main()
{
	IOS;
	int t; cin >> t;
	while (t--)
		solve();
	return 0;
}
posted @ 2024-10-18 19:24  WHUStar  阅读(9)  评论(0编辑  收藏  举报