HOJ 2430 -- Counting the algorithms (tree array + greedy)

Source: Internet
Author: User

HOJ 2430 -- Counting the algorithms (tree array + greedy)

 

Counting the algorithms
My Tags (Edit)
  Source: mostleg
  Time limit: 1 sec   Memory limit: 64 M

Submitted: 701, Accepted: 273

As most of the ACMers, wy's next target is algorithms, too. wy is clever, so he can learn most of the algorithms quickly. after a short time, he has learned a lot. one day, mostleg asked him that how many he had learned. that was really a hard problem, so wy wanted to change to count other things to distract mostleg's attention. the following problem will tell you what wy counted.

Given 2N integers in a line, in which each integer in the range from 1 to N will appear exactly twice. you job is to choose one integer each time and erase the two of its appearances and get a mark calculated by the differece of there position. for example, if the first 3 is in position 86 and the second 3 is in position 88, you can get 2 marks if you choose to erase 3 at this time. you shoshould notice that after one turn of erasing, integers 'positions may change, that is, vacant positions (without integer) in front of non-vacant positions is not allowed.

Input

There are multiply test cases. Each test case contains two lines.

The first line: one integer N (1 <= N <= 100000 ).

The second line: 2N integers. You can assume that each integer in [1, N] will appear just twice.

Output

One line for each test case, the maximum mark you can get.

Sample Input

 

31 2 3 1 2 331 2 3 3 2 1

Sample Output

 

69

Hint

We can explain the second sample as this. first, erase 1, you get 6-1 = 5 marks. then erase 2, you get 4-1 = 3 marks. you may notice that in the beginning, the two 2 s are at positions 2 and 5, but at this time, they are at positions 1 and 4. at last erase 3, you get 2-1 = 1marks. therefore, in total you get 5 + 3 + 1 = 9 and that is the best strategy.


 

 

--------------------Split line------------------

Question:

Given a 2n integer in the range of 1 to n, each number appears twice and each number has a subscript. Each time two identical numbers are deleted, the subscript difference value can be obtained, this is the most worthwhile

 

Ideas:

In the case of interlace, deleting from the periphery to the inside is the same as deleting from the inside out. In the case of inclusion, deleting from the outside to the inside is the best. Therefore, select Delete from outside to inside.

Specific operations:

Scan once, record the subscript that appears for the second time, and insert the subscript into the tree array. Then scan from left to right or from right to left, the value is the Distance Difference between the two numbers, and then delete one of them. If the scan is from left to right, delete the subscript that appears for the second time. If it is scanned from right to left, delete the subscript that appears for the first time.

 

 

 

#include
 
  #include
  
   #include
   
    #define ll long long#define M 200000+10using namespace std;int a[M],c[M],f[M];bool judge[M];int n;void update(int x,int v){    for(int i=x;i<=2*n;i+=i&-i){        c[i]+=v;    }}int getsum(int x){    int sum=0;    for(int i=x;i>0;i-=i&-i){        sum+=c[i];    }    return sum;}int main(){    while(scanf("%d",&n)!=EOF){        memset(judge,false,sizeof(judge));        memset(c,0,sizeof(c));        memset(f,0,sizeof(f));        for(int i=1;i<=2*n;++i){            scanf("%d",&a[i]);            update(i,1);            if(!judge[a[i]]) judge[a[i]]=true;            else f[a[i]]=i;        }        int sum=0;        for(int i=1;i<=2*n;++i){            sum+=getsum(f[a[i]])-getsum(i);            update(f[a[i]],-1);        }        printf("%d\n",sum);    }    return 0;}
   
  
 


 

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.