zoukankan      html  css  js  c++  java
  • [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度


    Description

    These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ≤ N ≤ 50000) numbers, which are no more than 109, Crazy Thair is a group of 5 numbers {i, j, k, l, m} satisfying:

    1. 1 ≤ i < j < k < l < m ≤ N
    2. Ai < Aj < Ak < Al < Am

    For example, in the sequence {2, 1, 3, 4, 5, 7, 6},there are four Crazy Thair groups: {1, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 3, 4, 5, 7} and {2, 3, 4, 5, 7}.

    Could you help Sempr to count how many Crazy Thairs in the sequence?

    Input

    Input contains several test cases. Each test case begins with a line containing a number N, followed by a line containing N numbers.

    Output

    Output the amount of Crazy Thairs in each sequence.

    Sample Input

    5
    1 2 3 4 5
    7
    2 1 3 4 5 7 6
    7
    1 2 3 4 5 6 7

    Sample Output

    1
    4
    21


    题目大意

    给你一个长度为 n 的序列,输出长度为5
    的上升子序列的个数。

    题解

    拿到这道题,可以很快的想出dp定义,
    dp[i][j] 表示以 j 这个数结尾能组成长度为 i 的上升子序列的个数。可以推出转移方程为

    (dp[i][j] = sum_{k=1}^{j-1}dp[i-1][k])

    i 最大为5 ,j可以达到(10^9),但个数只有(10^5),所以我们可以考虑将 j 离散化,开 5 个树状数组来维护。
    最后,这道题最坑的是,答案可能非常大,需要高精度。。。

    代码

    #include <iostream>
    #include <vector>
    #include <algorithm> 
    #include <cstring>
    #include <cstdio>
    using namespace std;
    #define lowbit(x) (x & -x)
    #define base 10000
    
    const int maxn = 5e4 + 5;
    int n;
    int a[maxn];
    vector <int> v;
    
    
    struct Bign {
    	int c[20],l;
    	Bign() {memset(c,0,sizeof(c));l = 1;}
    	void reset() {memset(c,0,sizeof(c));l = 1;}
    	
    	void Print() {
    		printf("%d",c[l]);
    		for(int i = l - 1;i > 0;i--)printf("%04d",c[i]);
    	}
    	
    	void operator = (const int &a) {
    		int x = a;
    		reset();
    		c[1] = x % base;x /= base;
    		while(x) {
    			c[++l] = x % base;x /= base;
    		}
    	}
    	
    	Bign operator + (const Bign &a) {
    		Bign res;
    		res.l = max(l,a.l);
    		for(int i = 1;i <= res.l;i++) {
    			res.c[i] += c[i] + a.c[i];
    			res.c[i+1] = res.c[i] / base;
    			res.c[i] %= base;
    		}
    		if(res.c[res.l+1])res.l++;
    		return res;
    	}
    	
    	Bign operator += (const Bign &a) {
    		*this = *this + a;
    		return *this;
    	}
    }t[5][maxn];
    	
    
    inline int getid(int x) {return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;}
    
    inline void update(int pos,int x,Bign a) {
    	while (x <= n) {
    		t[pos][x] += a;
    		x += lowbit(x);
    	}
    }
    
    inline Bign query(int pos,int x) {
    	Bign res;
    	while(x) {
    		res += t[pos][x];
    		x -= lowbit(x);
    	}
    	return res;
    }
    
    int main() {
    	ios::sync_with_stdio(false);cin.tie(0);
    	while(cin >> n) {
    		v.clear();
    		for(int i = 0;i < n;i++) {
    			cin >> a[i];v.push_back(a[i]);
    		}
    		Bign ans;
    		sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());
    		memset(t,0,sizeof(t));
    		for(int i = 0;i < n;i++) {
    			int x = getid(a[i]);
    			Bign xx;xx = 1;
    			update(0,x,xx);
    			for(int i = 1;i < 5;i++) {
    				update(i,x,query(i-1,x-1));
    			}
    		}
    		ans = query(4,n);
    		ans.Print();printf("
    ");
    	}
    	return 0; 
    } 
    
  • 相关阅读:
    模块cv2的用法
    调整弹出对话框在ASP.NET应用程序的大小
    xaf 自定义登陆页
    xaf 修改主页logo
    显示一个列表视图图表
    双击直接编辑状态
    xaf 富文本框添加方法
    用户 'NT AUTHORITYIUSR' 登录失败
    C# 中的INotifyPropertyChanged
    线程间操作无效: 从不是创建控件“txtreceive”的线程访问它。
  • 原文地址:https://www.cnblogs.com/ZegWe/p/5994082.html
Copyright © 2011-2022 走看看