这道题目的意思是对于每个要删除的数字,向前或向后找到一块连续的数字,而它是其中最小的;

很容易看出对于所有要先删除的数字要从大到小删除;

然后对于每个要删除的字母,要找到比他小的,但是在原数列中又靠它最近的数字;

这样的话,很直观最多只能用lg n的复杂度来处理这个问题;

可以用二分查找,也可以用set来代替;

考虑到前面删除的一些数字不能计算进去,还要一个快速计算区间和的算法,用树状数组和线段树都可以;

不过看到tags,上面写着还可以用dsu(disjoint set union)并查集来做;

感觉挺神奇的,想到了之后再补上!

#include<cstdio>
#include<cstring>
#include<set>
#include<iostream>
#define maxn 1000009
using namespace std;
int n,m;
int pos[maxn];
bool vis[maxn];
long long value[maxn];
set<int>st;
set<int>::iterator it; void add(int x,int v)
{
while(x<=n)
{
value[x]+=v;
x+=x&(-x);
}
} long long sum(int x)
{
long long ret=;
while(x>)
{
ret+=value[x];
x-=x&(-x);
}
return ret;
} int main()
{
int num;
long long ans=;
scanf("%d%d",&n,&m);
st.insert();
st.insert(n+);
for(int i=;i<=n;i++)
{
scanf("%d",&num);
pos[num]=i;
add(i,);
}
for(int i=;i<=m;i++)
{
scanf("%d",&num);
vis[num]=;
}
for(int i=;i<=n;i++)
{
if(vis[i]==)//need be moved
{
it=st.upper_bound(pos[i]);
int r=*it-;
int l=*(--it);
ans+=sum(r)-sum(l);
add(pos[i],-);
}
else
{
st.insert(pos[i]);
}
}
cout<<ans;
}

经过hza大神的指点才明白用并差集的方法来做;

其实这个思路也比较简单。

我们从小到大来顺序来消灭数字的时候,我们总是找到它左右两边比它小的数字;

由于是从小到大的顺序,所以找到的数组只可能是不能被消灭的;

这样的话,就可以把整个数组分成几个小片。

就可以用并差集来处理;

最后从大到小来处理结果;

#include<cstdio>
#include<iostream>
#define maxn 1000006
using namespace std;
int n,m;
int pos[maxn];
bool can[maxn];
int size[maxn];
int f[maxn];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} void dsu_union(int x,int y)
{
int a=find(x);
int b=find(y);
if(a==b)return;
f[b]=a;
size[a]+=size[b];
size[b]=;
} void interval_union(int p)
{
if(p>&&!can[p-])
dsu_union(p,p-);
if(p<n&&!can[p+])
dsu_union(p,p+);
} int main()
{
int num,p;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&num);
pos[num]=i;
f[i]=i;
}
for(int i=;i<=m;i++)
{
scanf("%d",&num);
can[pos[num]]=;
}
for(int i=;i<=n;i++)
if(!can[i])
interval_union(i);
long long ans=;
for(int i=n;i>=;i--)
{
p=pos[i];
num=find(p);
size[num]++;
if(!can[p])
ans+=size[num];
else
{
can[p]=;
interval_union(p);
}
}
cout<<ans;
}

codeforces 387C George and Number的更多相关文章

  1. Codeforces 467C George and Job(DP)

    题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...

  2. dp --- Codeforces 245H :Queries for Number of Palindromes

    Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H M ...

  3. Educational Codeforces Round 11 D. Number of Parallelograms 暴力

    D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...

  4. Codeforces 980 E. The Number Games

    \(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...

  5. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  6. codeforces B. George and Round 解题报告

    题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...

  7. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

  8. Codeforces C. Split a Number(贪心大数运算)

    题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output ...

  9. codeforces 467C.George and Job 解题报告

    题目链接:http://codeforces.com/problemset/problem/467/C 题目意思:给出一条含有 n 个数的序列,需要从中找出 k 对,每对长度为 m 的子序列,使得 找 ...

随机推荐

  1. [转]c#.NET和VB.NET语法的比较

    本文转自:http://www.cnblogs.com/lify0407/archive/2007/08/01/838589.html c#.NET和VB.NET语法的比较   VB.NET C# C ...

  2. Matlab plotyy函数的使用及问题总结

    MATLAB函数,用来绘制双纵坐标图. 调用格式: 1.plotyy(X1,Y1,X2,Y2):以左.右不同纵轴绘制X1-Y1.X2-Y2两条曲线. 2.plotyy(X1,Y1,X2,Y2,FUN1 ...

  3. js动态生成按钮,页面用DIV简单布局

    今天朋友让我忙帮给写个页面,由于时间紧破,所以没有完善,暂时先贴出来,以后有时间了在做修改 <!DOCTYPE html><html><head><title ...

  4. 汉诺塔的问题:4个柱子,如果塔的个数变位a,b,c,d四个,现要将n个圆盘从a全部移到d,移动规则不变

    四柱汉诺塔问题的求解程序.解题思路:如a,b,c,d四柱. 要把a柱第n个盘移到目标柱子(d柱),先把上层 分两为两部份,上半部份移到b柱,下半部分移到c柱,再把第n盘移到 目标柱子,然后,c柱盘子再 ...

  5. JAXB - Hello World

    We'll stick with the tradition and use a sort of "Hello World" XML document to illustrate ...

  6. C#学习笔记8:HTML和CSS基础学习笔记

    <!-- 1.<P>...</P>段落标签 2.<br/>折行标签. 3.<img src="" height="*px& ...

  7. AppWidgetProvider生命周期

    1.在桌面上添加小部件,让小部件可用会调用:onEnabled 2.作为通知会调用onReceive 3.小部件可用后会调用onUpdate,表明小部件状态由不可用变为可用发生了变化 4.作为通知又会 ...

  8. jquery实现asp.net 网页鼠标所在位置

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="mouseposition. ...

  9. ASP.NET 发送email

    首先添加命名空间 using System.Net.Mail; /// <summary> /// 发送邮件 /// </summary> /// <param name ...

  10. IIS7 发现无法显示ewebeditor编辑器成空白

    vs2003写的网站,很早了,编辑器用的是ewebeditor,每次更换程序编辑器都会出问题.今天记录一下. 内部老网站在Windows2003 iis6上运行的. 现在要迁移到2008上64位.08 ...