【POJ】3378 Crazy Thairs(树状数组+dp+高精)
2024-10-21 05:00:13
题目
传送门:QWQ
分析
题意:给个数列,求有多少五元上升组
考虑简化一下问题:如果题目求二元上升组怎么做。
仿照一下逆序对,用树状数组维护一下就ok了。
三元怎么做呢?
把二元的拓展一位就可以了,即把第三个也扔进树状数组
所以这题就渐渐明朗了:
用$ dp[i][x] $表示以$ A[x] $结尾的$ x $元上升组有多少个
那么:
$ dp[i][x]=\sum_{j=1}^{i-1} dp[j][x-1] (A[j]<A[i]) $
其中 $ dp[i][1]=1 $
因为多了一位大的就加了一位嘛
但这个看起来是$ O(n^2) $的,肯定要凉,所以扔进树状数组优化一下。
对了,这题还要离散化和高精度
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=,N = , Base = ;
typedef long long LL;
class BigNum {
public:
int num[], len;
BigNum():len() {}
BigNum(int n):len() { for( ; n > ; n /= Base) num[len++] = n%Base; }
BigNum Bigvalueof(LL n) {
len = ; while(n) { num[len++] = n%Base; n /= Base; }
return *this;
}
BigNum operator + (const BigNum& b) {
BigNum c; int i, carry = ;
for(i = ; i < this->len || i < b.len || carry > ; ++i) {
if(i < this->len) carry += this->num[i];
if(i < b.len) carry += b.num[i];
c.num[i] = carry%Base; carry /= Base;
}
c.len = i; return c;
}
BigNum operator += (const BigNum& b) { *this = *this + b; return *this; }
void Print() {
if(len == ) {puts(""); return ;}
printf("%d", num[len - ]);
for(int i = len - ; i >= ; --i)
for(int j = Base/; j > ; j /= )
printf("%d", num[i]/j%);
puts("");
}
};
typedef BigNum bign;
int n;
bign sum[maxn][];
struct Node{
int v,pos;
bool operator < (const Node& a) const{ return v<a.v; }
}a[maxn];
void add(int x,int e,bign a){for(;x<=n;x+=x&-x)sum[x][e]+=a;}
bign summ(int x,int e){bign ans;for(;x>;x-=x&-x)ans+=sum[x][e];return ans;}
int main(){
while(~scanf("%d",&n)){
for(int i=;i<=n;i++){scanf("%d",&a[i].v);a[i].pos=i;}
sort(a+,a++n); memset(sum,,sizeof(sum));
int cnt=;
bign ans=;
for(int i=;i<=n;i++){
add(a[i].pos,,);
for(int j=;j<=;j++){
add(a[i].pos,j,summ(a[i].pos-,j-));
}
}
summ(n,).Print();
}
return ;
}
最新文章
- Android NDK几点回调方式
- 自己制作QQ空间音乐的具体方法
- 黑马程序员_JAVA基础知识总结3
- ArcGIS与SuperMap的使用比较(1)
- HDU 1505 City Game (hdu1506 dp二维加强版)
- urlencode在url中的作用
- 互联网常见Open API文档资源
- Playing with ptrace, Part II
- HDU 5624 KK&#39;s Reconstruction 最小生成树
- poj2817WordStack(状压)
- ISP PIPLINE (七) gamma
- 使用 Asp.net core 2.0 + Angular 4 构建车辆管理的Web应用程序
- 20165305 苏振龙《Java程序设计》第四周学习总结
- 常用的apache commons工具,直接使用,便于快速开发
- 数组操作方法中的splice()和concat() 以及slice()
- python——脚本和print
- 图的最短路径---弗洛伊德(Floyd)算法浅析
- linux 安装谷歌浏览器
- Spring Roo 想知道源码,怎么实现自动生成枯燥的有规律的文件
- 关于Maven项目install时出现No compiler is provided in this environment的处理