贪心加树状数组

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

#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. appium操作微信公众号H5 web页面

    安卓微信公众号的H5页面是webview,一般操作需要切换context. 在执行如下步骤,就能直接像识别native样识别webview 1.代码追加: ChromeOptions options ...

  2. 【Android】10.3 网格视图(GridView)

    分类:C#.Android.VS2015: 创建日期:2016-02-19 一.简介 网格视图(GridView)是在GridLayout的基础上添加了滚动功能的视图,即:GridView用于在可滚动 ...

  3. Linux系统cp: omitting directory `XXX'问题解决

    在linux系统中复制文件夹时提示如下: cp: omitting directory `foldera/' 其中foldera是我要复制的文件夹名,出现该警告的原因是因为foldera目录下还存在目 ...

  4. .NET Garbage-Collectors

    http://mattwarren.org/tags/#Garbage-Collectors https://github.com/dotnet/coreclr/issues https://lldb ...

  5. compiled inline cache

    http://cr.openjdk.java.net/~jrose/pres/200910-VMIL.pdf https://wiki.openjdk.java.net/display/HotSpot ...

  6. ny2 括号配对问题

    括号配对问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在,有一行括号序列,请你检查这行括号是否配对.   输入 第一行输入一个数N(0<N<=1 ...

  7. MFC图形编辑器

    前言 vs2015竟然可以完美打开工程,哈哈可以直接生成类图了.由于内容较多,所以根据内容的重要性会安排详略. https://github.com/bajdcc/GraphEditor/releas ...

  8. 使用codedom 编写脚本解释器

    本篇博客的目的是为了保存例子,怕自己忘记. private void dd(string code) { string path = "BonkerSpace"; if (File ...

  9. java的regex问题笔记

    参考javadoc java.util.regex.Pattern 里面有一些说明,如果还有不明白的地方 yes,google it. @ “不能以0开头,1到多位数字,字符集为0到9” " ...

  10. 屏幕相关操作(XE10.1+WIN8.164)

    相关资料: http://www.bianceng.cn/Programming/Delphi/201104/25455.htm http://blog.csdn.net/anbangs/articl ...