"HOJ2430" "greedy + Tree-like array" counting the algorithms

Source: Internet
Author: User

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 had learned a lot. One day, Mostleg asked him so how many he had learned. That is really a hard-problem, so WY wanted-to-change-to-count other things to distract Mostleg' s attention. The following problem would tell you what WY counted.

Given 2N integers in a line, in which each integer in the range from 1 to N would appear exactly Twice. You are to choose one integer each time and erase the both of its appearances and get a mark calculated by the Differece of there position. For example, if the first 3 are in position and the second 3 are in position Can get 2 marks if choose to erase 3 on this time. You should notice this after one turn of erasing, integers ' positions ' could change, which is, vacant positions (without Integ ER) in front of non-vacant positions are not allowed.

Input

There is multiply test cases. Each test case contains the lines.

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

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

Output

One line for each test case, the maximum mark 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 2and you get 4-1=3 marks. Notice in the beginning, the 2s is at positions 2 and 5 They is at positions 1 and 4. At the last erase 3, you get 2-1=1 marks. Therefore, in total you get 5+3+1=9 and that's the best strategy.

Analysis

Comparing the water problem, according to test instructions, any two intervals are only intersecting or inclusive relationships.

Intersection regardless of the first delete which has no effect on the results, including of course to remove the outside of the first, so from the back to the next delete will not have the relationship.

A tree-like array can be implemented.

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <vector>6#include <utility>7#include <iomanip>8#include <string>9#include <cmath>Ten#include <map> One  A Const intMAXN =100000*2+Ten;  - using namespacestd; - structdata{ the        intL, R; -}NUM[MAXN *2]; - intDATA[MAXN *2]; - intC[MAXN *2], CNT[MAXN *2]; + intN; - BOOL Get[MAXN *2]; +  A intLowbit (intx) {returnx&-x;} at voidAddintXintval) { -       while(x <=2*N) { -C[X] + =Val; -X + =lowbit (x); -      } -      return; in } - intSumintx) { to     intCNT =0; +      while(X >0){ -CNT + =C[x]; theX-=lowbit (x); *     } $     returnCNT;Panax Notoginseng } - voidinit () { thec[0] =0; +       for(inti =1; I <=2N i++) C[i] = cnt[i] =0; A       for(inti =1; I <=2N i++){ thescanf"%d", &data[i]); +          if(Cnt[data[i]] = =0) {NUM[DATA[I]].L = i;cnt[data[i]]++;} -          ElseNUM[DATA[I]].R =i; $Add (I,1); $      } -      Long LongAns =0; -MemsetGet,0,sizeof(Get)); the       for(inti =2N I >=1; i--){ -          if(Get[I] = =1)Continue;WuyiAns + = SUM (i)-sum (NUM[DATA[I]].L); theAdd (I,-1); -Add (NUM[DATA[I]].L,-1); Wu          Get[I] =Get[NUM[DATA[I]].L] =0; -      } Aboutprintf"%lld\n", Ans); $ } -  - intMain () { -      intT =0;  A #ifdef LOCAL +Freopen ("Data.txt","R", stdin); theFreopen ("OUT.txt","W", stdout);  -      #endif  $       while(SCANF ("%d", &n)! =EOF) { the init (); the      } the      return 0; the } -  
View Code

"HOJ2430" "greedy + Tree-like array" counting the algorithms

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.