频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
HOJ 2430——Counting the algorithms(树状数组+贪心)
2014-08-29 10:57:38      个评论    来源:fsdcyr  
收藏   我要投稿

 

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


 

 

————————————————————分割线——————————————————

题目大意:

给定2n个整数,范围为1到n,每个数出现两次,每个数都有一个下标,每次删除两个相同的数,能获得下标差的值,求这个值得最大

 

思路:

对于相间的情况,从外围往内删跟从内往外删是一样的,而对于 包含的情况,从外往内删是最优的。因此,选择从外往内删。

具体操作:

扫描一次,记录每个数第二次出现的下标,将数的下标插人树状数组。然后从左往右或者从右往左扫描,那么值为两个数的距离差,之后删除其中一个数,如果是从左往右扫描的,则删除第二次出现的下标,如果是从右往左扫描的,则删除第一次出现的下标

 

 

 

#include
#include
#include
#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;
}


 

点击复制链接 与好友分享!回本站首页
相关TAG标签 数组
上一篇:LightOJ 1032 - Fast Bit Calculations (数位dp)
下一篇:Codeforces 156B. Suspects
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站