bzoj1106 [POI2007] cube big battle tet

Source: Internet
Author: User

Description

A game called Cube big Battle swept the whole Byteotia. The rules of this game are quite complex, so we'll just introduce his simple rules: Given the player a stack with 2n elements, the elements are placed one at a stacked place. These elements have n different numbers, with exactly two elements per number. The player can exchange two adjacent elements at a time. If, after the swap, the two adjacent elements are numbered identically, they are removed from the stack and all elements above them fall and can cause a chain reaction. The goal of the player is to eliminate all blocks with a minimum number of steps.

Input

The first line of the input file contains a positive integer n (1<=n<=50000). The next 2n line is a number AI per line, describing the entire stack from top to bottom, guaranteeing that each number appears and only occurs two times (1<=ai<=n). Initially, no two of the same elements are adjacent. and ensure that all data can be solved within 1000000 steps.

Output

The first line of the output file contains a number m, which represents the minimum number of steps.

Sample InputSample Input 1
5
5
2
3
1
4
1
4
3
5
2
Sample Input 2
3
1
2
3
1
2
3
Sample OutputSample Output 1
2
Sample Output 2
3HINT

For a string, we're going to merge its element exchange positions.

For merging, you can just delete these two elements, and the answer = = the distance between the two

Like what

Read directly from left to right, read the first occurrence of the number when the record position, the second occurrence of the time directly and the first merge out.

This kind of greed proves to be correct:

1, if there is such a string 12321, then the first merger of two 2 must be combined with two 1 is better than first

So if the position of the two pairs of elements is a nested relationship, then it is better to delete the middle

2, if there are 123456712 of strings, to merge 1, 2, then merge 1 First and Merge 2 is no difference

So we found that if there was a intersection between the two pairs of elements, it would be the same.

3, obviously if there is no intersection between the two elements must not affect each other

In conclusion, such greed is correct.

Finally, the tree-like array is used to maintain the distance.

#include <cstdio> #define LL long longinline int read () {    int x=0,f=1;char ch=getchar ();    while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}    return x*f;} int n; LL ans;int c[100010];int mrk[100010];inline int lowbit (int x) {return x& (-X);} inline void Add (int x,int d) {for    (int i=x;i<=2*n;i+=lowbit (i))        C[i]+=d;} inline int query (int x) {    int s=0;    for (int i=x;i;i-=lowbit (i))        s+=c[i];    return s;} int main () {    n=read ();    for (int i=1;i<=2*n;i++)    {        int x=read ();        if (Mrk[x])        {            ans+= (LL) query (i-1)-query (mrk[x]);            Add (mrk[x],-1);        } else        {            mrk[x]=i;            Add (i,1);        }    }    printf ("%lld\n", ans);    return 0;}

  

bzoj1106 [POI2007] cube big battle tet

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.