E. Subordinates【贪心】

E. Subordinates

题意:已知一张图的总祖先和每个顶点拥有的祖先个数,问至少需要修改几个,使得这张图满足情况

思路:模拟一下,大概就知道了吧。

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int MOD=1e9+7;
ll a[N],sum[N],dis[N];
int main(void){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    ll n,s;
    cin >>n>>s ;
    for(int i=1;i<=n;i++)   cin >> a[i];
    for(int i=1;i<=n;i++){
        if(a[i]==0&&i!=s)   a[i]=1e18+8;
    }
    vector<ll> t;
    ll ans=0;
    if(a[s]!=0) ans++;
    a[s]=0;
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)   t.pb(a[i]);
    ll now=1;
    for(int i=1;i<(int)t.size();i++){
        if(t[i]==now)   now++;
        else if(t[i]<now)       continue;
        else if(t[i]>now){
            while(t[i]>now){
                if((int)t.size()-1==i){
                    cout << ans+1 << endl;
                    return 0;
                }
                now++;
                t.erase(prev(t.end()));
                ans++;
            }
            now++;
        }
    }
    cout << ans << endl;

    return 0;
}
/*
5 1
0 0 1 3 4
*/

猜你喜欢

转载自blog.csdn.net/haipai1998/article/details/80964188