codeforces 387C George and Number
这道题目的意思是对于每个要删除的数字,向前或向后找到一块连续的数字,而它是其中最小的;
很容易看出对于所有要先删除的数字要从大到小删除;
然后对于每个要删除的字母,要找到比他小的,但是在原数列中又靠它最近的数字;
这样的话,很直观最多只能用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的更多相关文章
- Codeforces 467C George and Job(DP)
题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- Educational Codeforces Round 11 D. Number of Parallelograms 暴力
D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...
- Codeforces 980 E. The Number Games
\(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- codeforces B. George and Round 解题报告
题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...
- 【codeforces 805D】Minimum number of steps
[题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...
- Codeforces C. Split a Number(贪心大数运算)
题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output ...
- codeforces 467C.George and Job 解题报告
题目链接:http://codeforces.com/problemset/problem/467/C 题目意思:给出一条含有 n 个数的序列,需要从中找出 k 对,每对长度为 m 的子序列,使得 找 ...
随机推荐
- 关于Git的暂存区这个概念的理解.
Git中的暂存区成为stage或者是index.可以理解成一个"提交任务".Git暂存区是Git最成功的设计之一,但是也是最难理解的. 暂存区是一个介于工作区和版本库的中间状态.当 ...
- WinForm控件小知识
1.DataGridView控件显示自定义表 //造个数据表 DataTable dt = new DataTable(); dt.Columns.Add("DEcode", Sy ...
- Linq DataTable Group By 分组显示人员明细
实现功能: 多个字段分组源码样例: 原始数据: 分组后的输出结果: 源代码: public static void PrintPersons() { //准备数据 DataTable dt ...
- 使用info.plist(或工程名-info.plist)向程序中添加软件Build ID或者版本号信息
在实际应用程序开发过程中,经常需要向程序中添加软件版本号或者类似的信息,以保证之后发现问题时知道bug所在的版本,我们可以通过在工程名-info.plist文件中设置相关的key/value对(键/值 ...
- MVC构架思想
一.构架的基本思想 采用MVC构架一个网站时,最好随时随地地将脑袋中切割成三份(M,V,C),这是一个最基本的切割单位,而且也是最容易切割的三个部分,但是在实务上,通常不会这么简单,有时候我们会再多切 ...
- mysql net连接读取结果为乱码 Incorrect string value
在mysql直接查询中文正常,通过连接到mysql读取中文内容为乱码.同时插入中文内容也失败提示 Incorrect string value: '\xBC\xA4\xB7\xA2\xBF\xB4.. ...
- (转) VS2012程序打包部署详解
程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的是没有了打包工具.不知道出于什么原因微软没有将打包工具集成在开发环境中,但是我知道总会有解决办法的. 经过翻阅资料发现 ...
- oninput和onpropertychange
时常会有监听输入框输入的场景,比如新浪微博的发微博输入框: 还有边输入边提示: 以及form表单边输入边验证,当内容为空提示或者改变输入框的样式达到提示效果. 在IE中是onpropertychang ...
- 关于char 指针变量char *=p;这个语句的输出问题
学习指针的时候我一直有个疑惑,请看下面的代码: #include <iostream> using std::cout; void main() { ; int *nPtr=&nu ...
- [leetcode] 403. Frog Jump
https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...