贪心加树状数组

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

#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. Maven install 报错: Failed to execute goalorg.apache.maven.plugins:maven-gpg-plugin:1.4:sign (sign-art

    执行 Maven install 时报错: Failed to execute goalorg.apache.maven.plugins:maven-gpg-plugin:1.4:sign (sign ...

  2. yii2 数据库操作详解(转载)

    开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),DSN( Data Source Name ...

  3. Linux中telnet命令

    telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...

  4. Correlation and Regression

    Correlation and Regression Sample Covariance The covariance between two random variables is a statis ...

  5. [na]诺顿ghost磁盘对刻(备份系统分区或数据分区)

    一 诺顿ghost简介 ,可以克隆分区 也可以克隆磁盘 ,克隆成img或磁盘内容对刻 ,磁盘分区--img---磁盘分区 磁盘---磁盘 二 操作步骤 对刻好的系统 整体思路: ,A是模板机,A有C ...

  6. error: expected expression before 'struct'

    错误原因: 使用了offsetof函数,却没有包含头文件<stddef.h> 解决办法: 包含<stddef.h>

  7. SpringCloud重试机制配置

    SpringCloud重试retry是一个很赞的功能,能够有效的处理单点故障的问题.主要功能是当请求一个服务的某个实例时,譬如你的User服务启动了2个,它们都在eureka里注册了,那么正常情况下当 ...

  8. js中将文件的base64转换成file并上传到服务器

    ** * @param base64Codes * 图片的base64编码 */ function sumitImageFile(base64Codes){ var form=document.for ...

  9. VxWorks中logMsg与printf的区别

    printf( ) - write a formatted string to the standard output stream (ANSI). logMsg( ) does not actual ...

  10. cjson库

    - 源码方式引用,只有两个文件- 标准C89编写 - [cJSON 库项目地址](https://github.com/DaveGamble/cJSON)