Bzoj 1106 POI2007 cube large battle tet simulation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.