贪心加树状数组

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

#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. Mybatis里Mapper映射sql文件里insert的主键返回selectKey使用

    有时候新增一条数据,知道新增成功即可,但是有时候,需要这条新增数据的主键,以便逻辑使用,再将其查询出来明显不符合要求,效率也变低了. 这时候,通过一些设置,mybatis可以将insert的数据的主键 ...

  2. C#判断窗体是否存在重复打开

    foreach (Form f in Application.OpenForms) { f.Name //是打开窗体的Text //以下判断....... } Form2 F2 ; if(F2 == ...

  3. ASP.NET MVC 入门9、Action Filter 与 内置的Filter实现(介绍)

    原帖地址:http://www.cnblogs.com/QLeelulu/archive/2008/10/09/1307660.html 有时候你想在调用action方法之前或者action方法之后处 ...

  4. centos 7 中的 systemd

    systemd的服务管理程序 systemctl是最主要的工具.它融合 service 和chkconfig的功能于一体.你可以使用它永久性或只在当前会话中启用/禁用服务. 下面命令用于列出正在运行的 ...

  5. C#深入总结

    面向对象编程好处:提高软件可复用性.可扩展性.易维护性.灵活性第一章:理解.NET Framework与C# 1..NET框架由.NET Frmaework和CLR组成: 2.CLR(公共语言运行时) ...

  6. 多重连接的数据库管理工具:Navicat Premium

    多重连接的数据库管理工具:Navicat Premium 2016-09-26 Navicat Premium(非免费软件)是一个可多重连接的数据库管理工具,它可让你以单一程序同时连接到MySQL.O ...

  7. LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  8. LeetCode: Rotate Image 解题报告

    Rotate ImageYou are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...

  9. 关于github上开源nineoldandroids兼容动画的笔记

    github上面有个样例非常好的列举了NineOldAndroids兼容动画的使用方法.下载了下来然后执行測试了下: 由于要引用nineoldandroids的项目,我这里直接把它的源码拷贝进去了,用 ...

  10. 解决 Plugin with id 'com.github.dcendents.android-maven' not found.

    在Android studio中引用第三方库的时候,报这个错. Error:(2, 0) Plugin with id 'com.github.dcendents.android-maven' not ...