【数列处理】A. Era

题目来源

Problem - A - Codeforcesicon-default.png?t=L9C2https://codeforces.com/contest/1604/problem/A

 官方思路

解释 

让指针i进行遍历,只要找到max(ai-i)然后从头开始插入ai-i个元素就可以保证对于任意i,ai<=i)

本人mle代码

void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	for (auto& x : a)cin >> x;
	a.push_back(-1);
	int i = 0,count=0;
	while (a[i] != -1)
	{
		if (a[i] > i+1)
		{
			a.insert(a.begin() + i , i);
			count++;
		}
		i++;
	}
	cout << count << endl;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		 solve();
	}
}

官方源代码

#include<bits/stdc++.h>
using namespace std;
 
int main() {
  int t; cin >> t;
  while (t--) {
    int n; cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      int k; cin >> k;
      ans = max(ans, k - i);
    }
    cout << ans << '\n';
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/nathanqian123/article/details/121063294
今日推荐