贪心加树状数组

给出的数据可能出现两种情况,包括与不包括,但我们从右向左删就能避免这个问题。

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn=200010;
int f[maxn],l[maxn],a[maxn];
long long tree[maxn];
int n;
inline int lowbit(int x)
{
return x&(-x);
}
void add(int pos,int x)
{
for(int i=pos;i<=2*n;i+=lowbit(i))
tree[i]+=x;
}
int getsum(int pos)
{
int sum=0;
for(int i=pos;i>0;i-=lowbit(i))
sum+=tree[i];
return sum;
}
int main()
{
while(~scanf("%d",&n))
{
memset(f,0,sizeof(f));
memset(l,0,sizeof(l));
for(int i=1;i<=2*n;i++)
{
scanf("%d",&a[i]);
f[a[i]]==0?f[a[i]]=i:l[a[i]]=i;
add(i,1);
}
long long ans=0;
for(int i=1;i<2*n;i++)
{
//if(getsum(i)-getsum(i-1)>0)
//{
if(f[a[i]]==0) continue;
ans=ans+getsum(l[a[i]])-getsum(i-1)-1;
add(i,-1);
add(l[a[i]],-1);
f[a[i]]=0;
//}
}
printf("%lld\n",ans);
}
return 0;
}

hoj Counting the algorithms的更多相关文章

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

  2. 【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 ...

  3. hoj2430 Counting the algorithms

    My Tags   (Edit)   Source : mostleg   Time limit : 1 sec   Memory limit : 64 M Submitted : 725, Acce ...

  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. Spring 中属性配置

    1 注册自定义属性编辑器,方法一.使用BeanFactory, 则用户需要手动调用 registerCustomEditor(Class requiredType, PropertyEditor pr ...

  2. 关于TCP/IP与数据传输

    一.TCP/IP的具体含义: 从字面意思来讲,很多人会认为TCP/IP是指TCP与IP这两种协议.有时确实也可以说是这两种协议,但是大部分情况下所说的是利用IP进行通信时所必须用到的协议群的统称.具体 ...

  3. Spring依赖注入构造器注入(通过构造函数注入)

    在src目录下建立applicationContext.xml   (Spring 管理 bean的配置文件) <?xml version="1.0" encoding=&q ...

  4. 定时登录下载sftp服务器上的某些有规则的文件

    [root@1-live duizhangdan]# cat wget.sh #!/bin/bash#定时下载sina sftp服务器上的bak_dir=`date -d"5 day ago ...

  5. Eclipse中导入Git项目

    1.先将项目git到本地 2.导入刚刚git到本地项目 if(如果project带.calsspath .project 文件){ 直接用genaral导入或andorid project导入即可. ...

  6. 重温java中的String,StringBuffer,StringBuilder类

    不论什么一个系统在开发的过程中, 相信都不会缺少对字符串的处理. 在 java 语言中, 用来处理字符串的的类经常使用的有 3 个: String.StringBuffer.StringBuilder ...

  7. SyntaxError: missing ] after element list 火狐问题

    关于火狐运行var obj = eval('(' + data + ')');时 报SyntaxError: missing ] after element list错误,Chrome和IE正常 情形 ...

  8. vim:隆重推荐括号补全插件--auto-pairs

    太好用了,括号相关的各种麻烦都一一解决,剩下的就是熟练,熟练,在熟练了.呵呵 连教程都做得这么好,先放这里,以后慢慢翻译. Auto Pairs Insert or delete brackets, ...

  9. Flink articles

    http://ictlabs-summer-school.sics.se/2015/slides/flink-advanced.pdf http://henning.kropponline.de/20 ...

  10. C# 通过 HTTPModule 防范 DOS

    public class DosAttackModule : IHttpModule { void IHttpModule.Dispose() { } void IHttpModule.Init(Ht ...