贪心加树状数组

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

#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. Ext.net控件调整后台事件、方法论

    一.以ext.net的button为例调用后台事件: 前台代码: <ext:Button ID="Button1" runat="server" Text ...

  2. Java并发之AQS同步器学习

    AQS队列同步器学习 在学习并发的时候,我们一定会接触到 JUC 当中的工具,JUC 当中为我们准备了很多在并发中需要用到的东西,但是它们都是基于AQS(AbstractQueuedSynchroni ...

  3. OpenGL cullface

    opengl cullface是根据顶点顺逆时针来判断正反面的.而不是根据法线判断的.所以有可能法线是正确的,但cullface效果却是反的.

  4. Spring的三种注入方式(Setter、构造函数和自动注入)

    一.Setter注入 这里我是希望在Student.java中调用Course.java中的内容. public class Course { public String name = "数 ...

  5. AAA含义图解

    来源: <FreeRADIUS Beginner's Guide> 这本书 1,认证 2,授权 3,审计

  6. Sublime Text 3 快捷鍵

    /*On OS X, basic text manipulations (left, right, command+left, etc) make use of the system key bind ...

  7. Qt creator 编译错误:无法解析的外部符号(命令)

    问题来自于:仅仅是在creator 中加入了一个新的DIalog类,并在main(),中实例化并show.就出现例如以下的错误: main.obj:-1: error: LNK2019: 无法解析的外 ...

  8. Loadrunner常用操作

    LoadRunner 参数化 为什么需要参数化? 大众理解:为了更加真实的模拟用户操作 底层原理: 1,应用服务,数据库会校验该值的唯一性(unique key) 2,为了避免数据库的查询缓存对性能测 ...

  9. 李洪强iOS开发之-sql数据库的使用

    一,创建工程 二: 导入头文件 三:导入 四: 数据库增删改查 //因为是结构体类型,所以用assign //1.创建数据库(保存路径) @property(nonatomic,assign)sqli ...

  10. ubuntu tftp 配置

    1:sudo apt-get install tftp tftpd openbsd-inetd特别指出很多文章里用的是netkit-inetd,但是实际下载时发现这个软件是下不到的,特改用openbs ...