The main topic: given a length of 2n sequence, 1~n each appear two times, you can exchange adjacent two, two same number put together will cancel, the minimum number of times to cancel all the numbers
If there's a pair in the middle of the other pair, then this pair must be exchanged before the other pair.
The answer to this factor is certain.
Since the number of exchanges is guaranteed to be <=100w, it is possible to sweep from left to right, maintain a stack, and sequentially record elements that have not been erased.
If the new elements appear in the stack, go directly to the stack to find, delete the direct maintenance can be
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 50500using namespace Std;int n,ans;int stack[m],top;bool v[m];void Eliminate (int x) {int i;for (i=top;i;i--) if (stack[i]= =x) break;for (ans+=top-i;i<top;i++) stack[i]=stack[i+1];top--;} int main () {int i,x;cin>>n;for (i=1;i<=n<<1;i++) {scanf ("%d", &x), if (!v[x]) V[x]=true,stack[++top] =x;elseeliminate (x);} Cout<<ans<<endl;return 0;}
Bzoj 1106 POI2007 cube large battle tet simulation