zoukankan      html  css  js  c++  java
  • 最长递增子序列的数量 51Nod

    最长递增子序列的数量

    51Nod - 1376
     
    dp...用树状数组维护一下len和cnt
     
     1 //树状数组 dp
     2 //求LIS的数量
     3 #include <bits/stdc++.h>
     4 using namespace std;
     5 
     6 const int mod = 1e9 + 7;
     7 const int maxn = 50010;
     8 struct Node{
     9     int len, cnt;
    10     Node(int len = 0, int cnt = 0) : len(len), cnt(cnt){}
    11     Node operator + (const Node &t) const{
    12         if(len < t.len) return t;
    13         if(len > t.len) return (*this);
    14         return Node(len, (cnt + t.cnt) % mod);
    15     }
    16 };
    17 struct Info{
    18     int x, pos;
    19     bool operator < (const Info &temp) const{
    20         if(x == temp.x) return pos > temp.pos;
    21         return x < temp.x;
    22     }
    23 };
    24 
    25 bool cmp(Info &a, Info &b){
    26     if(a.x == b.x) return a.pos > b.pos;
    27     return a.x < b.x;
    28 }
    29 Node c[maxn];
    30 Info a[maxn];
    31 
    32 int n;
    33 void add(int i, Node v){
    34     for(; i <= n; i += i & -i) c[i] = c[i] + v;
    35 }
    36 
    37 Node sum(int i){
    38     Node res;
    39     for(; i; i -= i & -i) res = res + c[i];
    40     return res;
    41 }
    42 
    43 int main(){
    44     while(scanf("%d", &n) != EOF){
    45         for(int i = 0; i < n; i++){
    46             scanf("%d", &a[i].x);
    47             a[i].pos = i + 1;
    48         }
    49         sort(a, a + n);
    50         Node ans;
    51         for(int i = 0; i < n; i++){
    52             Node temp = sum(a[i].pos);
    53             if(++temp.len == 1) temp.cnt = 1;
    54             ans = ans + temp;
    55             add(a[i].pos, temp);
    56         }
    57         cout<<ans.cnt<<endl;
    58 
    59     }
    60 }
    View Code

    CDQ写法没看懂=_=

    学会了再回来补上。。。

  • 相关阅读:
    [CTF]zip伪加密
    Node.js躬行记(5)——定时任务的调试
    不一样的资产安全 3D 可视化平台
    冬季里有温度的 3D 可视化智慧供热系统
    公路项目建设可视化进度管理
    ElementUI时间选择控件提交的时间为UTC时间
    Orcal创建触发器
    Orcal常用查询实例集合
    代码优化风格分享
    查某月的天数
  • 原文地址:https://www.cnblogs.com/yijiull/p/8325887.html
Copyright © 2011-2022 走看看