51Nod1376 (dp + BIT // cdq分治)
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分治)的更多相关文章
- HDU5322 Hope(DP + CDQ分治 + NTT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5322 Description Hope is a good thing, which can ...
- 【BZOJ-1492】货币兑换Cash DP + 斜率优化 + CDQ分治
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 3396 Solved: 1434[Submit][Sta ...
- 斜率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] * ...
- bzoj 2244 [SDOI2011]拦截导弹(DP+CDQ分治+BIT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2244 [题意] 给定n个二元组,求出最长不上升子序列和各颗导弹被拦截的概率. [思路] ...
- BZOJ 2726: [SDOI2012]任务安排( dp + cdq分治 )
考虑每批任务对后面任务都有贡献, dp(i) = min( dp(j) + F(i) * (T(i) - T(j) + S) ) (i < j <= N) F, T均为后缀和. 与j有关 ...
- HDU 3507 Print Article(CDQ分治+分治DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3507 [题目大意] 将长度为n的数列分段,最小化每段和的平方和. [题解] 根据题目很容易得到dp ...
- 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& ...
- BZOJ 1492: [NOI2007]货币兑换Cash [CDQ分治 斜率优化DP]
传送门 题意:不想写... 扔链接就跑 好吧我回来了 首先发现每次兑换一定是全部兑换,因为你兑换说明有利可图,是为了后面的某一天两种卷的汇率差别明显而兑换 那么一定拿全利啊,一定比多天的组合好 $f[ ...
- bzoj3672/luogu2305 购票 (运用点分治思想的树上cdq分治+斜率优化dp)
我们都做过一道题(?)货币兑换,是用cdq分治来解决不单调的斜率优化 现在它放到了树上.. 总之先写下来dp方程,$f[i]=min\{f[j]+(dis[i]-dis[j])*p[i]+q[i]\} ...
随机推荐
- 《蹭课神器》Alpha版使用说明
<蹭课神器>是一款方便大学生蹭课的软件,目前实现了查询课表的功能,还没有实现搜索和提醒的功能.有待进一步的开发! 登录之后点击查询操作,查询课表. 课表显示如下
- 关于singleton的几个实现
public class Singleton { public static void main(String[] args) { Singleton s1 = Singleton.getInstan ...
- "一个程序员的生命周期"读后感
这篇文章中作者叙述了自己和大多数大学生或许都会面对的问题,即是会走过挺多的歪路,面临很多的困难和压力,但是作者却从未放弃自己真正追求的东西.对于一个过来人的经验之谈,我们应该吸取经验,在大学好好去奋斗 ...
- Spring配置常识
(1)数据源配置 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" ...
- 201306114357-实验3-C语言
#include<stdio.h>#include <stdlib.h>#include <time.h>main(){ int a,b,c,n,u,i,sum; ...
- [转] Linux有问必答:如何修复“sshd error: could not load host key”
编译自:http://ask.xmodulo.com/sshd-error-could-not-load-host-key.html作者: GOLinux 本文地址:https://linux.cn/ ...
- 被深信服上网行为管理器AC拒绝的操作如何正常访问
1.管理员登入帐号 2.如下图,在菜单[实时状态]-[上网行为监控]中,搜索指定IP的行为记录,找到被拒绝的数据 3.如下图,在菜单[系统管理]-[全局排除地址]中,增加不过滤的地址并提交即可
- [日常工作]vCenter下虚拟机设置与宿主机时间同步的方法
1. ESXi 能够实现CPU超售 同事开启多与CPU个数的虚拟机 不通的虚拟机采用了时间分片的处理, 所以有时候虚拟机内的时间可能会比宿主机的时间过的更慢, 越来越久之后虚拟机的时间就会比较离谱了. ...
- [读书笔记]Linux命令行与shell编程读书笔记02 环境变量以及其他
1. Linux的环境变量. 全局环境变量的查看 printenv 一个结果示例 XDG_SESSION_ID=354TERM=xtermSHELL=/bin/bashSSH_CLIENT=10.24 ...
- FuelPHP 系列(五) ------ Security 防御
项目中难免会有 form 提交,对用户输入的所有信息进行过滤,可以避免 XSS 攻击,防止 SQL 注入. 一.设置配置信息 首先在 config.php 文件中,对 security 相关信息进行设 ...