4408: [Fjoi 2016]神秘数
4408: [Fjoi 2016]神秘数
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 452 Solved: 273
[Submit][Status][Discuss]
Description
一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数。例如S={1,1,1,4,13},
1 = 1
2 = 1+1
3 = 1+1+1
4 = 4
5 = 4+1
6 = 4+1+1
7 = 4+1+1+1
8无法表示为集合S的子集的和,故集合S的神秘数为8。
现给定n个正整数a[1]..a[n],m个询问,每次询问给定一个区间[l,r](l<=r),求由a[l],a[l+1],…,a[r]所构成的可重复数字集合的神秘数。
Input
第一行一个整数n,表示数字个数。
第二行n个整数,从1编号。
第三行一个整数m,表示询问个数。
以下m行,每行一对整数l,r,表示一个询问。
Output
对于每个询问,输出一行对应的答案。
Sample Input
5
1 2 4 9 10
5
1 1
1 2
1 3
1 4
1 5
Sample Output
2
4
8
8
8
HINT
对于100%的数据点,n,m
<= 100000,∑a[i] <= 10^9
Source
【题解】:
暴力做法:首先将这一段排序,然后如果存在某一个数,这个数比它前面的数的前缀和至少大2,那么答案就是它前面那个数的前缀和+1。
然而我们要想优化,才可AC。
如果我们将这段数排序,并且已知前n个数的神秘数为x,即现在凑得的数的区间为[1,x],新加入的数为a,那么不难发现,我们凑得的数又得到了一段区间[a+1,a+x],那么如果a+1<=x,我们就可以拼上这两段,而神秘数变为a+x+1。
也即是说,我们有当前解ans,我们将所有小等ans的数加起来(其实根据前面所推应该是小于,但是写小等不会错,而且对于代码来说更好些,至于为什么不多赘述),如果sigma<ans说明出现了断裂处,即此时ans为答案。否则我们将ans变为sigma+1,继续更新答案。
时间复杂度0(nlogn*P),其中P为常数(当数列为斐波那契时会被卡到极限40)
题解转自网络。(毕竟太弱了。QAQ)
【代码】:
#include<cstdio>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>'') ch=getchar();
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x;
}
const int N=1e5+,M=N*;
int n,m,sz,ans,root[N],ls[M],rs[M],sum[M];
void insert(int &k,int last,int l,int r,int val){
k=++sz;
ls[k]=ls[last];
rs[k]=rs[last];
sum[k]=sum[last]+val;
if(l==r) return ;
int mid=l+r>>;
if(val<=mid) insert(ls[k],ls[last],l,mid,val);
else insert(rs[k],rs[last],mid+,r,val);
}
int query(int x,int y,int l,int r,int lim){
int mid=l+r>>;
if(r<=lim) return sum[y]-sum[x];
else if(lim<=mid) return query(ls[x],ls[y],l,mid,lim);
else return sum[ls[y]]-sum[ls[x]]+query(rs[x],rs[y],mid+,r,lim);
}
int main(){
n=read();
for(int i=;i<=n;i++) insert(root[i],root[i-],,1e9,read());
m=read();
for(int i=,l,r;i<=m;i++){
l=read();r=read();ans=;
for(int sigma;(sigma=query(root[l-],root[r],,1e9,ans))>=ans;ans=sigma+);
printf("%d\n",ans);
}
return ;
}
4408: [Fjoi 2016]神秘数的更多相关文章
- Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 128[Submit][Status ...
- BZOJ 4408: [Fjoi 2016]神秘数
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 464 Solved: 281[Submit][Status ...
- BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树
4408: [Fjoi 2016]神秘数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 Description 一个可重复数字集 ...
- ●BZOJ 4408 [Fjoi 2016]神秘数
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 题解: 主席树 首先,对于一些数来说, 如果可以我们可以使得其中的某些数能够拼出 1- ...
- BZOJ 4408: [Fjoi 2016]神秘数 [主席树]
传送门 题意: 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},8无法表示为集合S的子集的和,故集合S的神秘数为8.现给定n个正整数a[1]. ...
- bzoj 4408: [Fjoi 2016]神秘数 数学 可持久化线段树 主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=4299 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1 ...
- BZOJ 4408: [Fjoi 2016]神秘数 主席树 + 神题
Code: #include<bits/stdc++.h> #define lson ls[x] #define mid ((l+r)>>1) #define rson rs[ ...
- [BZOJ4408][Fjoi 2016]神秘数
[BZOJ4408][Fjoi 2016]神秘数 试题描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1 ...
- 【BZOJ4408】[Fjoi 2016]神秘数 主席树神题
[BZOJ4408][Fjoi 2016]神秘数 Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1 ...
随机推荐
- ubuntu添加开机启动
vim /etc/init.d/mytest #!/bin/sh echo "$(pwd) and $USER and $(whoami)" >> /root/temp ...
- 九度oj 1011
题目描述: 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= ...
- BZOJ 4318 OSU! ——期望DP
这次要求$x^3$的概率和. 直接维护三个值$x$ $x^2$ $x^3$的期望. 概率的平方不等于平方的概率. #include <map> #include <ctime> ...
- cf725F Family Photos
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family phot ...
- cf287D Shifting
John Doe has found the beautiful permutation formula. Let's take permutation p = p1, p2, ..., pn. Le ...
- localStorage增删改查
/** * 设置 本地缓存 */ export function setStorage(key, obj) { if (typeof obj === 'string') { localStorage. ...
- shell高级-----初识sed和gawk
sed编辑器 sed说明 sed是Linux下一款功能强大的非交互流式文本编辑器,可以对文本文件进行增.删.改.查等操作,支持按行.按字段.按正则匹配文本内容,灵活方便,特别适合于大文件的编辑. 替换 ...
- Material Theme
Material Theme提供了一下功能: 1.系统widgets可以设置调色板 2.系统widgets的触摸反馈 3.Activity过渡动画 你可以根据你品牌的色彩来定义Material The ...
- 使用azure send grid发送email
1. create a send grid account 2. remember the username/password of the send grid account watermark/2 ...
- 【从零学习openCV】IOS7下的openCV开发起步(Xcode5.1.1&openCV2.49)
前言: 开发IOS7已经有一月的时间了.近期在准备推研的事,有点想往CV方向发展.于是開始自学openCV. 关注CSDN已经非常久了.也从非常多博主那学到了非常多知识,于是我也从这周开启自己的blo ...