降雷皇 【NOIP2017提高组模拟12.10】

来源:互联网 发布:同业拆借 知乎 编辑:程序博客网 时间:2024/05/23 01:14

题目

降雷皇哈蒙很喜欢雷电,他想找到神奇的电光。
哈蒙有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 longusing 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);}

这里写图片描述

0 0
原创粉丝点击