HOJ2430 Counting the Algorithms 树状数组

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 first3 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 should 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

3
1 2 3 1 2 3
3
1 2 3 3 2 1

Sample Output

6
9

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 2s 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=1 marks. Therefore, in total you get 5+3+1=9 and that is the best strategy.



题目大意:有一个长度为2*N的数列,其中1~N的每个整数都出现2次,要将它们两两删除,删除一个元素以后,另一个元素也被消除,并获得等于它们位置标号之差的绝对值的分数,问删除完所有元素之后所能获得的最大分数是多少。


两种情况。

第一种 a b b a  我们选择先选a,再选b。这样获得的值最大。

第二种 a b a b 这个随意,我们可以先选a,可以先选b,结果是一样的。

所以,我们直接从左边开始选即可。


我们建立一个树状数组来表示距离,每个点都设为1.

ab之间的距离就是ab之间1的个数。


我们统计每个数字最后一次出现的位置。

比如案例 1 2 3 1 2 3;

数字1最后一次出现在4位置。

数字2最后一次出现在5位置。

数字3最后一次出现在6位置。

我们从左边开始访问。

i=1,数字1,我们查到1最后一次出现在4位置。 我们访问1到4的长度,也就是3,sum+=3;我们让 第二个出现的那个1位置-1.也就是删掉了。

至于i=1这个位置,删不删都无所谓,因为已经遍历过去了,我们不会再碰到他了。  



#include<iostream>  
#include<cstring>  
#include<cstdio>  
#define ll long long  
#define M 200000+10  
using 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;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值