递推计数-hdu-4747-Mex
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4747
题目大意:
给n个数,求所有区间内没有出现的最小非负整数和。
解题思路:
首先感谢大神博客:http://www.cnblogs.com/xin-hua/p/3325154.html可以解释的太少了。
我再解释一遍。
首先要明白,以i结束的所有区间的值的和记为f[i]肯定不超过以i+1结束的所有区间的值的和记为f[i+1]。
所以可以根据f[i]间接推出f[i+1],记第i个数为sa[i],显然只用考虑大于等于sa[i]的数j对f[i]=f[i-1]+?的影响,。如果j出现在1~i-1区间中,比较j最晚出现的位置与覆盖完全的1~j-1的最小位置的较小位置k,那么区间j的前一次出现的位置到k位置这个区间内所有点到i位置的值都+1.
这样逐次累加,直到不影响为止。
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#define eps 1e-6
#define INF 0x3fffffff
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 210000 int sa[Maxn],pos[Maxn],full[Maxn]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n; while(scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
scanf("%d",&sa[i]);
memset(pos,0,sizeof(pos));
memset(full,0,sizeof(full));
int last;
ll tt=0,ans=0; for(int i=1;i<=n;i++)
{
if(sa[i]<n)//
{
last=pos[sa[i]];//前一个sa[i]的最晚位置
pos[sa[i]]=i; //最晚位置
for(int j=sa[i];j<n;j++)
{
if(j) //考虑j对前面区间的影响
full[j]=min(full[j-1],pos[j]); //
else
full[j]=i;
if(full[j]>last)
tt+=full[j]-last; //last+1到full[j]区间内所有点到i的值+1,逐次累加
else
break;
}
}
printf("i:%d %I64d\n",i,tt);
ans+=tt;
}
printf("%I64d\n",ans);
}
return 0;
}
递推计数-hdu-4747-Mex的更多相关文章
- HDU 4747 Mex 递推/线段树
题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...
- ACM学习历程—HDU5396 Expression(递推 && 计数)
Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...
- 【HDU 4747 Mex】线段数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:有一组序列a[i](1<=i<=N), 让你求所有的mex(l,r), mex ...
- [HDU 4747] Mex (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 这道题是我去年刚入校队的时候参加网赛的题目. 一年过去了,我依然还是不会做.. 这是我难题计划的 ...
- HDU 4747 Mex(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:给出一个数列A.计算所有的mex(i,j)之和.1<=i<=j<=n. ...
- hdu 4747 Mex (2013 ACM/ICPC Asia Regional Hangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 思路: 比赛打得太菜了,不想写....线段树莽一下 实现代码: #include<iost ...
- hdu 4747 mex 线段树+思维
http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...
- hdu 4747 Mex
http://acm.hdu.edu.cn/showproblem.php?pid=4747 设我们输入的数组为 a[],我们需要从 1 到 n 遍历, 假设遍历到 i 时, 遍历的过程中用b[j]表 ...
- HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Problem Description Mex is a function on a set of integers, which is universally used for impartial ...
随机推荐
- Python 函数 切片 迭代 列表生成器
函数 编写 定义一个函数要用def语句 def sum(i,n): ⚠有冒号 返回多值 实际上是返回一个tuple 定义默认参数 默认参数的作用是简化调用 def ...
- AprioriTID algorithm
What is AprioriTID? AprioriTID is an algorithm for discovering frequent itemsets (groups of items ap ...
- LoadRunner Tutorial
LoadRunner Tutorial Welcome to the LoadRunner tutorial. The tutorial is a self-paced guide that lead ...
- rails跑通第一个demo
rails -h 查看帮助 Usage: rails new APP_PATH [options] Options: -r, [--ruby=PATH] # Path to the Ruby bina ...
- tftp使用方法
参数说明:-l 是local的缩写,后跟存在于Client的源文件名,或下载Client后 重命名的文件名. -r 是remote的缩写,后跟Se ...
- 【POJ 1182 食物链】并查集
此题按照<挑战程序设计竞赛(第2版)>P89的解法,不容易想到,但想清楚了代码还是比较直观的. 并查集模板(包含了记录高度的rank数组和查询时状态压缩) *; int par[MAX_N ...
- hdu 5569 matrix(简单dp)
Problem Description Given a matrix with n rows and m columns ( n+m ,) and you want to go to the numb ...
- ROS Node/Topic/Message/Service的一些问题
1.Node http://blog.exbot.net/archives/1412 (摘自老王说ros) node干的什么活?callback queue里的活.这个callback queue里的 ...
- EnyimMemcached扩展 遍历功能
Memcached本身对外提供的命令不多,也就add.get.set.incr.decr.replace.delete.stats等几个,客户端对这些操作进行了封装,总体来说调用还是很简单的. 初看了 ...
- springmvc 传递和接收数组参数
java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...