递推计数-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 ...
随机推荐
- .Net MVC4 加.html后缀报404问题
一.配置方法: routes.MapRoute( name: "User_Default1", url: "user/{action}.html", defau ...
- ui原则
http://www.niushe.com/news/show-3683.html 设计师Joshua Porter发表了一篇文章——<Principles of User Interface ...
- C#、VB.NET 使用System.Media.SoundPlayer播放音乐
原文:C#.VB.NET 使用System.Media.SoundPlayer播放音乐 同步播放: System.Media.SoundPlayer player = new System.Media ...
- haproxy redirect prefix
acl short_domain hdr(Host) -i etiantian.org redirect prefix http://www.etiantian.org code 301 if sho ...
- 如何把Python2的代码转换为Python3的代码
如何把Python2的代码转换为Python3的代码 注: 如果对于python2和python3不熟悉的,可以参考: [整理]总结Python2(Python 2.x版本)和Python3(Pyth ...
- 蜂鸟A20开发板刷 cubietruck 的 SD 卡固件
美睿视讯 为蜂鸟A20准备的 MerriiLinux 功能非常简陋.所以能用上主流的 debian 或者 LUbuntu 就可以说是非常迫切的需求了.蜂鸟A20(Merrii Hummingbird ...
- hdu2574 Hdu Girls' Day (分解质因数)
Hdu Girls' Day Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- linux监控脚本,脚本支持传参,整合C程序
1,查看指定用户下的进程pid
- php信用卡卡号验证函数
介绍一个php信用卡卡号验证函数,可以验证一个卡号是否是信用卡. function validateCard ($cardnumber){ $cardnumber = preg_replace (&q ...
- OC基础16:复制对象
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.浅复制即是指针赋值,复制对象的修改会影 ...