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

Submitted : 725, Accepted : 286

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.

这道题可以用树状数组做,用map<int,int>hash,来储存相同的数第二次出现的位置,这样待会更新的时候回比较方便,然后这里用到了贪心策略,即依次从左到右进行循环,找出相同的两个数,然后求出两个位置的差,然后删除这两个位置。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int a[200006],b[200006],n,vis[200006];
int lowbit(int x){
return x&(-x);
}
void update(int pos,int num)
{
while(pos<=2*n){
b[pos]+=num;pos+=lowbit(pos);
}
} int getsum(int pos)
{
int num=0;
while(pos>0){
num+=b[pos];pos-=lowbit(pos);
}
return num;
} int main()
{
int m,i,j,t,sum;
while(scanf("%d",&n)!=EOF)
{
map<int,int>hash;
hash.clear();
for(i=1;i<=2*n;i++){
vis[i]=0;
scanf("%d",&a[i]);
hash[a[i]]=i;
b[i]=lowbit(i);
}
sum=0;
for(i=1;i<=2*n;i++){
if(vis[i]==1)continue;
vis[i]=1;
t=hash[a[i]];
vis[t]=1;
sum+=getsum(t)-getsum(i);
update(i,-1);update(t,-1);
//printf("%d\n",sum);
}
printf("%d\n",sum);
}
return 0;
}

hoj2430 Counting the algorithms的更多相关文章

  1. 【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 th ...

  2. HOJ——T 2430 Counting the algorithms

    http://acm.hit.edu.cn/hoj/problem/view?id=2430 Source : mostleg Time limit : 1 sec Memory limit : 64 ...

  3. hoj Counting the algorithms

    贪心加树状数组 给出的数据可能出现两种情况,包括与不包括,但我们从右向左删就能避免这个问题. #include<stdio.h> #include<string.h> #inc ...

  4. [Algorithms] Counting Sort

    Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...

  5. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

  6. [算法]Comparison of the different algorithms for Polygon Boolean operations

    Comparison of the different algorithms for  Polygon Boolean operations. Michael Leonov 1998 http://w ...

  7. [zt]Which are the 10 algorithms every computer science student must implement at least once in life?

    More important than algorithms(just problems #$!%), the techniques/concepts residing at the base of ...

  8. The Aggregate Magic Algorithms

    http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...

  9. Top 10 Algorithms for Coding Interview--reference

    By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...

随机推荐

  1. 浅谈Go中的time.After

    go的一条哲学是 不要通过共享来实现通信,而是通信来实现共享 多协程之间通过 channel 来实现通信,而普遍会遇到的问题是,如何进行超时控制,资料一查询,需要配置select和time.After ...

  2. MyBatis初级实战之四:druid多数据源

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. 使用line_profiler对python代码性能进行评估优化

    性能测试的意义 在做完一个python项目之后,我们经常要考虑对软件的性能进行优化.那么我们需要一个软件优化的思路,首先我们需要明确软件本身代码以及函数的瓶颈,最理想的情况就是有这样一个工具,能够将一 ...

  4. js reduce数组转对象

    借鉴:https://juejin.im/post/5cfcaa7ae51d45109b01b161#comment这位大佬的处理方法很妙,但是我一眼看过去没有明白,细细琢磨了下,终于明白了 1 co ...

  5. druid discard long time none received connection问题解析

    最新项目中用的druid连接数据库遇到一个困扰很久的问题 1 开始用的druid版本是1.1.22版本,由于业务需求,单个连接需要执行很久,理论上不需要用到自动回收,但为了安全,还是加了自动回收,时间 ...

  6. 前端开发好帮手,eslint配置全知道

    eslint让人又爱又恨,原因在于它的默认配置非常严格,动则一个小提示就直接报错不给运行.而在开发调试的过程中,我们想时时得到运行效果,它的严格又很烦. 在安装eslint后,我们可以在package ...

  7. [Usaco 2012 Feb]Nearby Cows

    题目描述 FJ发现他的牛经常跑到附近的草地去吃草,FJ准备给每个草地种足够的草供这个草地以及附近草地的奶牛来吃.FJ有N个草地(1<=N<=100000),有N-1条双向道路连接这些草地, ...

  8. Percona Toolkit工具使用

    Percona Toolkit简称pt工具-PT-Tools,是Percona公司开发用于管理MySQL的工具,功能包括检查主从复制的数据一致性.检查重复索引.定位IO占用高的表文件.在线DDL等 下 ...

  9. 【.NET 与树莓派】矩阵按键

    欢迎收看火星卫视,本期节目咱们严重探讨一下矩阵按键. 所谓矩阵按键,就是一个小键盘(其实一块PCB板),上面有几个 Key(开关),你不按下去的时候,电路是断开的,你按下去电路就会接通.至于说有多少个 ...

  10. jquery 数据查询

    jquery 数据查询 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...