Conquers "more than 2019 college entrance exam."

The meaning of problems

According to legend, the East China Sea Dragon Palace has a peerless magic --n Zhang miracle cards, have written a number on each card. When Conquers want the underwater magic cards to destroy these n cards, if the current number of remaining cards is k, the Rebels can cast all the numbers for the cards k destruction, such operations can be carried out until no card can be destroyed so far.

But the Rebels found only carry out such an operation will be impossible to eliminate all the cards, so he borrowed Taiyizhenren master of magic, once the numbers on a card can be changed to any one he wanted other numbers.

But the East China Sea Dragon King found the Rebels attempt, so the East China Sea Dragon King will be m revision operation, the i-th the first \ (x_i \) Zhang card numbers changed to \ (y_i \) , the East China Sea Dragon King once every operation later, the Rebels want to know how many times a magic weapon to use at least until all the cards can be eliminated.


Thinking

Set \ (cnt [i] \) is the number of i appearance, then i will obviously be deleted after the \ (i-cnt [i] +1 \) , then maintain it on connectivity to each location.

Code

#include <bits/stdc++.h>

using namespace std;

namespace StandardIO {
    
    template<typename T> inline void read (T &x) {
        x=0;T f=1;char c=getchar();
        for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
        for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
        x*=f;
    }
    template<typename T> inline void write (T x) {
        if (x<0) putchar('-'),x=-x;
        if (x>=10) write(x/10);
        putchar(x%10+'0');
    }
    
}

using namespace StandardIO;

namespace Solve {
    
    const int N=200200;
    
    int n,m,ans;
    int a[N],cnt[N],tot[N];
    
    inline void MAIN () {
        read(n),read(m);
        for (register int i=1; i<=n; ++i) {
            read(a[i]),++cnt[a[i]];
            if (a[i]-cnt[a[i]]+1>0) if ((++tot[a[i]-cnt[a[i]]+1])==1) ++ans;
        }
        while (m--) {
            int x,y;
            read(x),read(y);
            if (a[x]-cnt[a[x]]+1>0) if ((--tot[a[x]-cnt[a[x]]+1])==0) --ans;
            --cnt[a[x]],a[x]=y,++cnt[a[x]];
            if (a[x]-cnt[a[x]]+1>0) if ((++tot[a[x]-cnt[a[x]]+1])==1) ++ans;
            write(n-ans),putchar('\n');
        }
    }
    
}

int main () {
    Solve::MAIN();
}

Guess you like

Origin www.cnblogs.com/ilverene/p/11418760.html