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

【分析】

比较水的一道题,根据题意可知,任何两个区间仅为相交或包含的关系。

相交无论先删除哪一个对结果都没有影响,包含当然要先删除外面的那一个,所以从后往前删不会有包含的关系。

具体用树状数组实现就可以了。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <map> const int MAXN = * + ;
using namespace std;
struct DATA{
int l, r;
}num[MAXN * ];
int data[MAXN * ];
int C[MAXN * ], cnt[MAXN * ];
int n;
bool get[MAXN * ]; int lowbit(int x){return x&-x;}
void add(int x, int val){
while (x <= * n){
C[x] += val;
x += lowbit(x);
}
return;
}
int sum(int x){
int cnt = ;
while (x > ){
cnt += C[x];
x -= lowbit(x);
}
return cnt;
}
void init(){
C[] = ;
for (int i = ; i <= * n; i++) C[i] = cnt[i] = ;
for (int i = ; i <= * n; i++){
scanf("%d", &data[i]);
if (cnt[data[i]] == ) {num[data[i]].l = i;cnt[data[i]]++;}
else num[data[i]].r = i;
add(i, );
}
long long Ans = ;
memset(get, , sizeof(get));
for (int i = * n; i >= ; i--){
if (get[i] == ) continue;
Ans += sum(i) - sum(num[data[i]].l);
add(i, -);
add(num[data[i]].l, -);
get[i] = get[num[data[i]].l] = ;
}
printf("%lld\n", Ans);
} int main(){
int T = ;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while (scanf("%d", &n) != EOF){
init();
}
return ;
}

【HOJ2430】【贪心+树状数组】 Counting the algorithms的更多相关文章

  1. 【bzoj4240】有趣的家庭菜园 贪心+树状数组

    题目描述 对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物.JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N.IOI草一共有N株,每个区域种植着一株.在第i个区域种 ...

  2. 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D

    http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...

  3. D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

    题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个 ...

  4. [BZOJ4240]有趣的家庭菜园(贪心+树状数组)

    最后数列一定是单峰的,问题就是最小化最后的位置序列的逆序对数. 从大到小加数,每次贪心看放左边和右边哪个产生的逆序对数更少,树状数组即可. 由于大数放哪对小数不产生影响,所以正确性显然. 注意相同数之 ...

  5. [P4064][JXOI2017]加法(贪心+树状数组+堆)

    题目描述 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个区间里选出恰好 ...

  6. [luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)

    传送门 贪心...蒟蒻证明不会... 每一次找最大的即可,找出一次最大的,数列会分为左右两边,左边用stl优先队列维护,右边用树状数组维护.. (线段树超时了....) 代码 #include < ...

  7. codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组

    题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...

  8. [JZO6401]:Time(贪心+树状数组)

    题目描述 小$A$现在有一个长度为$n$的序列$\{x_i\}$,但是小$A$认为这个序列不够优美. 小$A$认为一个序列是优美的,当且仅当存在$k\in [1,n]$,满足:$$x_1\leqsla ...

  9. UVALive 6911---Double Swords(贪心+树状数组(或集合))

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. 【转】XCode、Cocoa、Objective-C 的关系区别

    原文网址:http://blog.sina.com.cn/s/blog_5e89e1ff0100z4k1.html Object-Ciphone开发用的编程语言不是c,c++,java 而是objec ...

  2. dedecms网站如何做在线订单功能

    做网站的时候经常会遇到做在线订单的这个功能,而且这个功能会在企业网站的建设中经常的遇到,今天51模板集就拿物流网的在线订单功能做一个详细的介绍. 第一步:自定义表单 打开后台:核心-->自定义表 ...

  3. Java Web中web.xml的作用

    每一个javaWeb工程都有一个web.xml配置文件,那么他到底有什么作用呢?它是每一个web工程都必的必须的吗?   web.xml文件是用来初始化工程配置信息的,比如说welcome页面,fil ...

  4. 2013=7=12 ACM培训第一天

    ACM培训第一天,尽管我嘴上说是来打酱油的,但我非常想学好.1.一定要多思考,多总结:2.多问同学 :3.学会向女生说话,大胆,自信.(今天有女生向我说话了,很高兴.她很大胆,我要向她学习...... ...

  5. SDN基础理解

    本文转载自:http://blog.csdn.net/freezgw1985/article/details/16873677 个人觉得对很适合对SDN的入门级的概念性理解,先暂时copy一下,等研究 ...

  6. 《A First Course in Mathematical Modeling》-chaper2-建模过程、比例性及几何相似性

    这一章节着重从整体的层面给出数学建模过程中一个泛式流程,它给出了在给现实模型建立数学模型的框架性思路,但是需要注意的是,虽然这里称其为一种“泛式”思路,但是在具体的问题的分析中,整个建模过程还是充满了 ...

  7. centos下apache thrift的安装

    参考:http://running.iteye.com/blog/1983463  thrift-0.9.0安装 最好切换到root用户操作,避免不必要的麻烦. 进行例子程序tutorial目录下,通 ...

  8. Little Kings - SGU 223(状态压缩)

    题目大意:在一个N*N的棋盘上放置M个国王,已知国王会攻击与它相邻的8个格子,要求放置的额国王不能相互攻击,求放置的方式有多少种. 分析:用dp[row][state][nOne],表示本行状态sta ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(39)-在线人数统计探讨

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(39)-在线人数统计探讨 系列目录 基于web的网站在线统计一直处于不是很精准的状态!基本上没有一种方法可 ...

  10. vim note(6)--vim的一个较全的介绍(转)

    vim的配置文件 ~/.vimrc       用户的默认配置文件 ~/.vim/plugin/   用户的默认脚本文件的存放文件夹 ~/.vim/ftplugin/ 用户的默认文件类型相关脚本文件的 ...