降雷皇 【NOIP2017提高组模拟12.10】
2016-12-14 21:33
435 查看
题目
降雷皇哈蒙很喜欢雷电,他想找到神奇的电光。哈蒙有n条导线排成一排,每条导线有一个电阻值,神奇的电光只能从一根导线传到电阻比它大的上面,而且必须从左边向右传导,当然导线不必是连续的。
哈蒙想知道电光最多能通过多少条导线,还想知道这样的方案有多少。
样例输入
第一行两个整数n和type。type表示数据类型
第二行n个整数表示电阻。
5 1
1 3 2 5 4
样例输出
第一行一个整数表示电光最多能通过多少条导线。
如果type=1则需要输出第二行,表示方案数,对123456789取模。
3
4
数据范围
对于20%的数据n<=10;
对于40%的数据n<=1000;
对于另外20%的数据type=0;
对于另外20%的数据保证最多能通过不超过100条导线;
对于100%的数据n<=100000,电阻值不超过100000。
剖解题目
给一个序列,求其最长上升子序列的长度已经方案数。解法
通常的二分求法gg了。。。一颗权值线段树,维护前i个数中,以每一个数值结尾的子序列最大长度以及方案数。
假设当前数为a[i],那么就在1~a[i]-1中寻找出最大的长度以及其方案数。
更新到a[i]这个位置即可。
时间O(log n)
代码
#include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<cmath> #define fo(i,a,b) for(int i=a;i<=b;i++) #define ll long long using namespace std; const int maxn=1e5; const ll mo=123456789; struct cy{ int val; ll sum; }tree[maxn*4+10]; int n,type; ll num,maxx,ansm,ansn; void change(int p,int l,int r,int x) { if (l==r){ if (maxx>tree[p].val) tree[p].val=maxx,tree[p].sum=num; else if (maxx==tree[p].val) tree[p].sum=(tree[p].sum+num)%mo; return ; } int mid=(l+r)/2; if (x<=mid) change(p<<1,l,mid,x); else change(p<<1|1,mid+1,r,x); tree[p].val=max(tree[p<<1].val,tree[p<<1|1].val); if (tree[p<<1].val>tree[p<<1|1].val) tree[p].sum=tree[p<<1].sum; else if (tree[p<<1].val<tree[p<<1|1].val) tree[p].sum=tree[p<<1|1].sum; else tree[p].sum=(tree[p<<1].sum+tree[p<<1|1].sum)%mo; } void find(int p,int l,int r,int a,int b) { if(l==a&&r==b){ if (tree[p].val>maxx) maxx=tree[p].val,num=tree[p].sum; else if (tree[p].val==maxx) num=(tree[p].sum+num)%mo; return; } int mid=(l+r)/2; if (b<=mid) find(p<<1,l,mid,a,b); else if (a>mid) find(p<<1|1,mid+1,r,a,b); else { find(p<<1,l,mid,a,mid); find(p<<1|1,mid+1,r,mid+1,b); } } int main() { freopen("hamon.in","r",stdin); freopen("hamon.out","w",stdout); scanf("%d%d",&n,&type); maxx=0; num=1; change(1,0,maxn,0); fo(i,1,n){ int x; scanf("%d",&x); maxx=0; num=0; find(1,0,maxn,0,x-1); maxx++; change(1,0,maxn,x); if (maxx>ansm) ansm=maxx,ansn=num; else if (maxx==ansm) ansn=(ansn+num)%mo; } printf("%d\n",ansm); if(type) printf("%lld\n",ansn); }
相关文章推荐
- 利用C语言来求最大连续子序列乘积的方法
- LIS 最长递增子序列 Java的简单实现
- 【数据结构】线段树总结(一)
- 线段树题集
- 线段树
- 【日常练习 线段树】HDU1754 I hate it
- hdu1754
- HDU1394
- 敌兵布阵 (1)
- I Hate It (1)
- LCIS (2)
- A Simple Problem with Integers (2)
- Mayor's posters (3)
- Buy Tickets (3)
- 线段树
- UVA - 12532 Interval Product
- bzoj 4499
- POJ 3264 Balanced Lineup
- hdu 1542 求矩形并的面积
- 关于数据结构之线段树