upc Brick Break#模拟

问题 K: Brick Break
时间限制: 1 Sec 内存限制: 128 MB

题目描述
We have N bricks arranged in a row from left to right.
The i-th brick from the left (1≤i≤N) has an integer ai written on it.
Among them, you can break at most N−1 bricks of your choice.
Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1≤i≤K), the i-th of those brick from the left has the integer i written on it.
Find the minimum number of bricks you need to break to satisfy Snuke’s desire. If his desire is unsatisfiable, print -1 instead.

Constraints
·All values in input are integers.
·1≤N≤200000
·1≤ai≤N
输入
Input is given from Standard Input in the following format:

N
a1 a2 … aN
输出
Print the minimum number of bricks that need to be broken to satisfy Snuke’s desire, or print -1 if his desire is unsatisfiable.
样例输入 Copy
【样例1】
3
2 1 2
【样例2】
3
2 2 2
【样例3】
10
3 1 4 1 5 9 2 6 5 3
【样例4】
1
1
样例输出 Copy
【样例1】
1
【样例2】
-1
【样例3】
7
【样例4】
0
提示
样例1解释
If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.
样例2解释
In this case, there is no way to break some of the bricks to satisfy Snuke’s desire.

/**
就是一个很简单的模拟
搞懂题意就ok 
**/
int n,now,ans;
int a[maxn];
int main()
{
	n = read();
	for(int i=0;i<n;i++)	cin >> a[i];
	now = 1;
	for(int i=0;i<n;i++)
	{
		if(a[i] == now)
			now++;//如果是,那么下一个应该是now+1 
		else
			ans++;//如果不是,那么这一块砖应该敲掉 
	}
	if(now == 1)
		cout << -1 << endl;
	else 
		cout << ans << endl;
	return 0;
}
发布了42 篇原创文章 · 获赞 0 · 访问量 651

猜你喜欢

转载自blog.csdn.net/magic_wenge/article/details/105120829
UPC