https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1376

求LIS的数量。

乍一看觉得还是dp,仔细一看确实可以用dp做。

显而易见的是一个O(n2)的dp,同时维护LIS的值和cnt的数量 当然,由于数据限制,考虑优化

我们看了题解冷静分析之后想到了用树状数组优化。

用一个结构体node来存储len和cnt两个关键信息,重载他们之间的+运算符

struct Node{
LL cnt,len;
Node(){}
Node(int len,int cnt):len(len),cnt(cnt) {}
Node operator + (Node t){
if(t.len > this->len){
return t;
}else if(t.len < this->len){
return (*this);
}else{
return Node(t.len,(t.cnt + this->cnt) % mod);
}
}
}node[maxn];

看起来就很帅,实际上这是一个偏向比较的运算符,两个node之间长度不相等的就取长度较大的,长度相等就将两个cnt加起来。

我们将所有的数从大到小排序,并且大小相同时编号后面的靠前,保证在遍历每个数时树状数组里所有的数都可以当作他的前缀最大值。直接上dp递推即可。

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int MAXBUF=;char buf[MAXBUF],*ps=buf,*pe=buf+;
inline bool isdigit(const char& n) {return (n>=''&&n<='');}
inline void rnext(){if(++ps==pe)pe=(ps=buf)+fread(buf,sizeof(char),sizeof(buf)/sizeof(char),stdin);}
template <class T> inline bool in(T &ans){
#ifdef VSCode
ans=;T f=;register char c;
do{c=getchar();if ('-'==c)f=-;}while(!isdigit(c)&&c!=EOF);
if(c==EOF)return false;do{ans=(ans<<)+(ans<<)+c-;
c=getchar();}while(isdigit(c)&&c!=EOF);ans*=f;return true;
#endif
#ifndef VSCode
ans =;T f=;if(ps==pe)return false;do{rnext();if('-'==*ps)f=-;}
while(!isdigit(*ps)&&ps!=pe);if(ps==pe)return false;do{ans=(ans<<)+(ans<<)+*ps-;
rnext();}while(isdigit(*ps)&&ps!=pe);ans*=f;return true;
#endif
}const int MAXOUT=; //*(int(*)[10])p
char bufout[MAXOUT], outtmp[],*pout = bufout, *pend = bufout+MAXOUT;
inline void write(){fwrite(bufout,sizeof(char),pout-bufout,stdout);pout = bufout;}
inline void out_char(char c){*(pout++)=c;if(pout==pend)write();}
inline void out_str(char *s){while(*s){*(pout++)=*(s++);if(pout==pend)write();}}
template <class T>inline void out_int(T x) {if(!x){out_char('');return;}
if(x<)x=-x,out_char('-');int len=;while(x){outtmp[len++]=x%+;x/=;}outtmp[len]=;
for(int i=,j=len-;i<j;i++,j--) swap(outtmp[i],outtmp[j]);out_str(outtmp);}
template<typename T, typename... T2>
inline int in(T& value, T2&... value2) { in(value); return in(value2...); }
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
#define Vec Point
typedef vector<int> VI;
const double eps = 1e-;
const int maxn = 5e4 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,tmp,K;
PII a[maxn];
struct Node{
LL len,cnt;
Node(){}
Node(LL len,LL cnt):len(len),cnt(cnt){}
Node operator + (const Node &t){
if(this->len < t.len){
return t;
}
if(this->len > t.len){
return (*this);
}
return Node(t.len,(this->cnt + t.cnt) % mod);
}
}tree[maxn];
int lowbit(int t){
return t & (-t);
}
void update(int t,Node x){
while(t <= N){
tree[t] = tree[t] + x;
t += lowbit(t);
}
}
Node query(int t){
Node s = Node(,);
while(t > ){
s = s + tree[t];
t -= lowbit(t);
}
return s;
}
bool cmp(PII a,PII b){
if(a.fi != b.fi) return a.fi < b.fi;
return a.se > b.se;
}
int main()
{
in(N);
For(i,,N) in(a[i].fi),a[i].se = i;
sort(a + ,a + + N,cmp);
Node ans(,);
For(i,,N){
Node t = query(a[i].se - );
if(++t.len == ) t.cnt = ;
t.len %= mod;
ans = ans + t;
update(a[i].se,t);
}
Prl(ans.cnt);
#ifdef VSCode
write();
system("pause");
#endif
return ;
}

这题也可以变成一个二维偏序问题,寻找一个在len长度最大情况下的cnt,用cdq分治解决。

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int MAXBUF=;char buf[MAXBUF],*ps=buf,*pe=buf+;
inline bool isdigit(const char& n) {return (n>=''&&n<='');}
inline void rnext(){if(++ps==pe)pe=(ps=buf)+fread(buf,sizeof(char),sizeof(buf)/sizeof(char),stdin);}
template <class T> inline bool in(T &ans){
#ifdef VSCode
ans=;T f=;register char c;
do{c=getchar();if ('-'==c)f=-;}while(!isdigit(c)&&c!=EOF);
if(c==EOF)return false;do{ans=(ans<<)+(ans<<)+c-;
c=getchar();}while(isdigit(c)&&c!=EOF);ans*=f;return true;
#endif
#ifndef VSCode
ans =;T f=;if(ps==pe)return false;do{rnext();if('-'==*ps)f=-;}
while(!isdigit(*ps)&&ps!=pe);if(ps==pe)return false;do{ans=(ans<<)+(ans<<)+*ps-;
rnext();}while(isdigit(*ps)&&ps!=pe);ans*=f;return true;
#endif
}const int MAXOUT=; //*(int(*)[10])p
char bufout[MAXOUT], outtmp[],*pout = bufout, *pend = bufout+MAXOUT;
inline void write(){fwrite(bufout,sizeof(char),pout-bufout,stdout);pout = bufout;}
inline void out_char(char c){*(pout++)=c;if(pout==pend)write();}
inline void out_str(char *s){while(*s){*(pout++)=*(s++);if(pout==pend)write();}}
template <class T>inline void out_int(T x) {if(!x){out_char('');return;}
if(x<)x=-x,out_char('-');int len=;while(x){outtmp[len++]=x%+;x/=;}outtmp[len]=;
for(int i=,j=len-;i<j;i++,j--) swap(outtmp[i],outtmp[j]);out_str(outtmp);}
template<typename T, typename... T2>
inline int in(T& value, T2&... value2) { in(value); return in(value2...); }
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
#define Vec Point
typedef vector<int> VI;
const double eps = 1e-;
const int maxn = 5e4 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,tmp,K;
int a[maxn];
struct Node{
LL cnt,len;
Node(){}
Node(int len,int cnt):len(len),cnt(cnt) {}
Node operator + (Node t){
if(t.len > this->len){
return t;
}else if(t.len < this->len){
return (*this);
}else{
return Node(t.len,(t.cnt + this->cnt) % mod);
}
}
}node[maxn];
int id[maxn];
bool cmp(int x,int y){
if(a[x] == a[y]) return x > y;
return a[x] < a[y];
}
void cdq(int l,int r){
if(l == r) return;
int m = l + r >> ;
cdq(l,m);
For(i,l,r) id[i] = i;
sort(id + l,id + r + ,cmp);
Node MAX = Node(,);
For(i,l,r){
int idx = id[i];
if(idx <= m){
MAX = MAX + node[idx];
}else{
Node t = MAX;
t.len++;
node[idx] = node[idx] + t;
}
}
cdq(m + ,r);
}
int main()
{
in(N);
For(i,,N) in(a[i]);
For(i,,N) node[i] = Node(,);
cdq(,N);
Node MAX = Node(,);
For(i,,N) MAX = MAX + node[i];
Prl(MAX.cnt);
#ifdef VSCode
write();
system("pause");
#endif
return ;
}

51Nod1376 (dp + BIT // cdq分治)的更多相关文章

  1. HDU5322 Hope(DP + CDQ分治 + NTT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5322 Description Hope is a good thing, which can ...

  2. 【BZOJ-1492】货币兑换Cash DP + 斜率优化 + CDQ分治

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 3396  Solved: 1434[Submit][Sta ...

  3. 斜率dp cdq 分治

    f[i] = min { f[j] + sqr(a[i] - a[j]) } f[i]= min { -2 * a[i] * a[j] + a[j] * a[j] + f[j] } + a[i] * ...

  4. bzoj 2244 [SDOI2011]拦截导弹(DP+CDQ分治+BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2244 [题意] 给定n个二元组,求出最长不上升子序列和各颗导弹被拦截的概率. [思路] ...

  5. BZOJ 2726: [SDOI2012]任务安排( dp + cdq分治 )

    考虑每批任务对后面任务都有贡献, dp(i) = min( dp(j) + F(i) * (T(i) - T(j) + S) ) (i < j <= N)  F, T均为后缀和. 与j有关 ...

  6. HDU 3507 Print Article(CDQ分治+分治DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3507 [题目大意] 将长度为n的数列分段,最小化每段和的平方和. [题解] 根据题目很容易得到dp ...

  7. bzoj1492--斜率优化DP+cdq分治

    显然在某一天要么花完所有钱,要么不花钱. 所以首先想到O(n^2)DP: f[i]=max{f[i-1],(f[j]*r[j]*a[i]+f[j]*b[i])/(a[j]*r[j]+b[j])},j& ...

  8. BZOJ 1492: [NOI2007]货币兑换Cash [CDQ分治 斜率优化DP]

    传送门 题意:不想写... 扔链接就跑 好吧我回来了 首先发现每次兑换一定是全部兑换,因为你兑换说明有利可图,是为了后面的某一天两种卷的汇率差别明显而兑换 那么一定拿全利啊,一定比多天的组合好 $f[ ...

  9. bzoj3672/luogu2305 购票 (运用点分治思想的树上cdq分治+斜率优化dp)

    我们都做过一道题(?)货币兑换,是用cdq分治来解决不单调的斜率优化 现在它放到了树上.. 总之先写下来dp方程,$f[i]=min\{f[j]+(dis[i]-dis[j])*p[i]+q[i]\} ...

随机推荐

  1. JQuery监听页面滚动总结

    1.当前滚动的地方的窗口顶端到整个页面顶端的距离: var winPos = $(window).scrollTop(); 2.获取指定元素的页面位置: $(val).offset().top; 3. ...

  2. StringBuffer的append方法比“+”高效

    在字符串的连接过程中StringBuffer的效率要比String高: string操作代码: String str = new String("welcome to "); st ...

  3. <<浪潮之巅>>阅读笔记一

    第一次的阅读就想读这本书的,却因为很多愿意一直拖到现在,因为听说这本书在李开复先生 的微博上有推荐,更是增加了我的阅读兴趣.可能是因为在网上找的电子版的原因,通篇阅读的速度很快,但是没有纸质数那种细嚼 ...

  4. Linux Network Commands

    https://www.tecmint.com/linux-network-configuration-and-troubleshooting-commands/ http://www.tldp.or ...

  5. 解决AJAX应用,会话超时(Session Timeout)的问题,粗略方法(不考虑使用Filter的前提下)

    function ajaxPost(url, data) { var async = false; var result = {}; $.ajax({ url : url, async : async ...

  6. pcntl php多进程

    <?php $i=0;while($i!=5){ $pid = pcntl_fork(); if ($pid == 0) { echo $pid."---------hahah&quo ...

  7. php函数值传值/地址以及引用的用法

    博客摘自  奔跑的大白,网址: http://www.cnblogs.com/gauze/p/5568867.html 1.先来解释一下名词. 值传递(passl-by-value)过程中,被调函数的 ...

  8. 二叉排序树类的: C++ 实现

    #include<iostream> using namespace std; template<class T> struct TreeNode { T element; T ...

  9. java程序在windows系统作为服务程序运行

    Java程序很多情况下是作为服务程序运行的,在Un*x 平台下可以利用在命令后加“&”把程序作为后台服务运行,但在Windows下看作那个Console窗口在桌面上,你是否一直担心别的同时把你 ...

  10. 同步工具:CountDownLatch、CyclicBarrier和Semaphore

    1. CountDownLatch 1.1 功能及使用场景 一个同步工具,使得一个或多个线程等待一组线程执行完成后再执行. 使用场景:等待一些前置任务执行完成后,再执行特定的功能.比如,系统启动时,各 ...