Brick Break——AT

题目描述

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

【样例13
2 1 2
【样例23
2 2 2
【样例310
3 1 4 1 5 9 2 6 5 3
【样例41
1

样例输出 Copy

【样例11
【样例2-1
【样例37
【样例40

提示

样例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.

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
ll gcd(ll a,ll b)
{
    ll t;
    while(b!=0)
    {
        t=a%b;
        a=b;
        b=t;
    }
    return a;
}
ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}
start{
    int n=read;
    int a[n];
    int ans = 0;
    int now = 1;
    for(int i=0;i<n;i++){
        a[i]=read;
        if(a[i]==now)
            now++;
        else
            ans++;
    }
    if(now==1)
        cout << -1 << endl;
    else
        cout << ans << endl;
    end;
}
 
/**************************************************************
    Language: C++
    Result: 正确
    Time:23 ms
    Memory:2688 kb
****************************************************************/
原文地址:https://www.cnblogs.com/PushyTao/p/13144180.html