[POJ 3378] Crazy Thairs
Link:
Solution:
按序列长度$dp$,
设$dp[i][j]$为到第$i$个数,符合要求的序列长度为$j$时的序列个数,
易得转移方程:$dp[i][j]=\sum_{k=1}^{i-1} dp[k][j-1] (dat[k]<dat[i])$
用树状数组按$dat[i]$为坐标来维护$dp[i][j-1]$的值即可,
由于$dat[i] \le 1e9$,记得离散化,同时答案超过$long long$,要高精度
这里可以选择开5个树状数组,也可以只用一个树桩数组,每次维护$dp[][j-1]$的值后清空
由于此题卡空间,推荐我用的第二种
如果要用第一种,要用到高精度的一些黑科技,改为10000进制,这样只要7位就行了Orz
10000进制的输出:
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("");
}
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std;
const int MAXN=5e4+;
int T,dsp[MAXN],n,dat[MAXN],tot=; struct BI //高精度类
{
int d[],len; BI() {memset(d,,sizeof(d));len=;}
void clean(){memset(d,,sizeof(d)),len=;}
BI(int num) {*this=num;} BI& operator = (const int& num)
{
memset(d,,sizeof(d));
int temp=num;len=;
while(temp)
d[++len]=temp%,temp/=;
return *this;
} BI operator + (const BI& num)
{
BI ret;ret=*this;
ret.len=max(len,num.len);
for(int i=;i<=ret.len;i++)
{
ret.d[i]+=num.d[i];
if(ret.d[i]>=)
ret.d[i]-=,ret.d[i+]++;
}
if(ret.d[ret.len+]) ret.len++;
return ret;
}
BI operator += (const BI& num){*this=*this+num;return *this;}
void print(){for(int i=len;i>=;i--) printf("%d",d[i]);}
}bit[MAXN],dp[MAXN][],res; void Update(int pos,BI val){while(pos<=n) bit[pos]+=val,pos+=pos&(-pos);}
BI Query(int pos)
{
BI ret=; //记得初始化
while(pos) ret+=bit[pos],pos-=pos&(-pos);
return ret;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d",&dat[i]),dsp[i]=dat[i]; //离散化
sort(dsp+,dsp+n+);tot=unique(dsp+,dsp+n+)-dsp-;
for(int i=;i<=n;i++) dat[i]=lower_bound(dsp+,dsp+tot+,dat[i])-dsp; res.clean();
for(int i=;i<n+;i++) for(int j=;j<;j++) dp[i][j].clean();
for(int i=;i<=n;i++) dp[i][]=(BI);
for(int i=;i<=;i++)
{
for(int j=;j<tot+;j++) bit[j].clean();
for(int j=;j<=n;j++)
dp[j][i]=Query(dat[j]-),Update(dat[j],dp[j][i-]);
}
for(int i=;i<=n;i++) res+=dp[i][];
res.print();puts("");
}
return ;
}
Review:
一道比较常规的题吧,用到了离散化和高精度的一些套路
(1)如果$dp$复杂度有问题,可以向单调性/斜率优化/RMQ维护上想一想
(2)犯的丝帛错误:
$Query$函数里的$ret$要预处理!!!
(3)黑科技:高精度空间不够时转为更高进制(10000进制)
[POJ 3378] Crazy Thairs的更多相关文章
- ●POJ 3378 Crazy Thairs
题链: http://poj.org/problem?id=3378 题解: 树状数组维护,高精度. 依次考虑以每个位置结尾可以造成的贡献. 假设当前位置为i,为了达到5个元素的要求,我们需要求出,在 ...
- POJ 3378 Crazy Thairs(树状数组+DP)
[题目链接] http://poj.org/problem?id=3378 [题目大意] 给出一个序列,求序列中长度等于5的LIS数量. [题解] 我们发现对于每个数长度为k的LIS有dp[k][i] ...
- poj 3378 Crazy Thairs dp+线段树+大数
题目链接 题目大意: 给出n个数, 让你求出有多少个5元组满足 i < j < k < l < m并且ai < aj < ak < al < am 我们 ...
- 【POJ】3378 Crazy Thairs(树状数组+dp+高精)
题目 传送门:QWQ 分析 题意:给个数列,求有多少五元上升组 考虑简化一下问题:如果题目求二元上升组怎么做. 仿照一下逆序对,用树状数组维护一下就ok了. 三元怎么做呢? 把二元的拓展一位就可以了, ...
- [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)
树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...
- poj 1200 Crazy Search(hash)
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...
- POJ 1200 Crazy Search(字符串简单的hash)
题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. ...
- POJ – 1200 Crazy Search
http://poj.org/problem?id=1200 #include<iostream> #include<cstring> using namespace std; ...
- poj 3378 二维树状数组
思路:直接用long long 保存会WA.用下高精度加法就行了. #include<map> #include<set> #include<cmath> #inc ...
随机推荐
- Java基础-1简单了解与原理
简单了解: Java看起来设计得很像C++,但是为了使语言小和容易熟悉,设计者们把C++语言中许多可用的特征去掉了,这些特征是一般程序员很少使用的.因为Java没有结构,数组和串都是对象,所以不需要指 ...
- django-settings里redis连接与缓存配置
# Django-redis的缓存配置 CACHES = { "default": { "BACKEND": "django_redis.cache. ...
- GCC 7.3 for Windows
GCC 6.1x Compilers 下载地址1: Mingw gcc 6.30下载 这个是某微软员工编译的版本 MinGW is a port of GCC to Windows. It is fr ...
- 哈希UVALive 6326 Contest Hall Preparation
Encrypting passwords is one of the most important problems nowadays, and y ...
- PHP面向对象练习2
思路:构造函数完成数据库连接,增删改一个方法,查询一条记录一个方法,查询多条一个方法,sql执行失败则返回提示,并交出sql语句方便查错 代码: <?class dbcontroll{ priv ...
- iOS大神班笔记03-UIApplication
UIApplication简介: UIApplication对象是应用程序的象征. 每一个应用程序都有自己的UIApplication对象,而且是单例. 一个iOS程序启动后创建的第一个对象就是UIA ...
- 用jQuery制作仿网易云课堂导航菜单效果
最近做项目,用到类似的效果. 效果图如下: 直接上代码: HTML: <!DOCTYPE html> <html lang="en"> <head&g ...
- [bzoj1033] [ZJOI2008]杀蚂蚁antbuster
Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...
- 最大异或和(xor)
最大异或和(xor) 题目描述 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.A x:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x: ...
- 2016华中农业大学预赛 B 数学
Problem B: Handing Out Candies Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 272 Solved: 20[Submit ...