POJ 3378 树状数组+DP+离散化+高精度
题意:给你一个序列,求其中长度为5的递增序列的个数(N<=50000)
这题的dp的方法挺经典的,方程大家应该都会写(那个N^2的),我就不再赘述,这里巧妙地运用了树状数组求和
c[i][j]表示当前状态时,以j(j是数字,不是下标)为结尾的长度为i的序列个数
剩下的就是考验高精度了~1100+ms
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <iostream> 5 #include <algorithm> 6 7 #define SIZE 10 8 9 using namespace std; 10 11 struct BIGN 12 { 13 int a[SIZE]; 14 }; 15 16 inline BIGN operator +(BIGN a,BIGN b) 17 { 18 BIGN c; 19 memset(&c,0,sizeof c); 20 c.a[0]=max(b.a[0],a.a[0]); 21 int jin=0; 22 for(int i=1;i<=c.a[0];i++) 23 { 24 jin+=a.a[i]+b.a[i]; 25 c.a[i]=jin%10000; 26 jin/=10000; 27 } 28 if(jin) c.a[++c.a[0]]=jin; 29 return c; 30 } 31 32 inline void give(char s[],BIGN &a) 33 { 34 memset(&a,0,sizeof a); 35 int len=strlen(s); 36 int p[4]={1,10,100,1000}; 37 for(int i=len-1,j=0;i>=0;i--,j=(j+1)&3) 38 { 39 if(!j) a.a[0]++; 40 a.a[a.a[0]]=a.a[a.a[0]]+(s[i]-'0')*p[j]; 41 } 42 } 43 44 inline void prt(BIGN a) 45 { 46 printf("%d",a.a[a.a[0]]); 47 for(int i=a.a[0]-1;i>=1;i--) printf("%04d",a.a[i]); 48 puts(""); 49 } 50 51 BIGN c[6][50010],ans; 52 int n,bh; 53 54 struct BZ 55 { 56 int x,h,id; 57 }bz[50010]; 58 59 void read() 60 { 61 for(int i=1;i<=n;i++) 62 { 63 scanf("%d",&bz[i].x); 64 bz[i].id=i; 65 } 66 } 67 68 inline bool cmp_x(const BZ &a,const BZ &b) 69 { 70 return a.x<b.x; 71 } 72 73 inline bool cmp_id(const BZ &a,const BZ &b) 74 { 75 return a.id<b.id; 76 } 77 78 inline int lowbit(int x) 79 { 80 return x&-x; 81 } 82 83 void lsh() 84 { 85 sort(bz+1,bz+1+n,cmp_x); 86 bz[1].h=2; bh=2; 87 for(int i=2;i<=n;i++) 88 { 89 if(bz[i].x!=bz[i-1].x) bz[i].h=++bh; 90 else bz[i].h=bh; 91 } 92 sort(bz+1,bz+1+n,cmp_id); 93 } 94 95 inline BIGN getsum(int p,int num) 96 { 97 BIGN rt; 98 give("0",rt); 99 while(num) 100 { 101 rt=rt+c[p][num]; 102 num-=lowbit(num); 103 } 104 return rt; 105 } 106 107 inline void updata(int p,int q,BIGN sy) 108 { 109 while(q<=bh) 110 { 111 c[p][q]=c[p][q]+sy; 112 q+=lowbit(q); 113 } 114 } 115 116 void go() 117 { 118 BIGN one,tmp; give("1",one); give("0",ans); 119 for(int i=0;i<=bh;i++) 120 for(int j=1;j<=5;j++) 121 give("0",c[j][i]); 122 for(int i=1;i<=n;i++) 123 { 124 updata(1,bz[i].h,one); 125 for(int j=2;j<=5;j++) 126 { 127 tmp=getsum(j-1,bz[i].h-1); 128 updata(j,bz[i].h,tmp); 129 } 130 } 131 ans=getsum(5,bh); 132 prt(ans); 133 } 134 135 int main() 136 { 137 while(scanf("%d",&n)!=EOF) 138 { 139 read(); 140 lsh(); 141 go(); 142 } 143 return 0; 144 }
发现数组开大了,一直tle,搞了半天才AC。。。
转载于:https://www.cnblogs.com/proverbs/archive/2012/10/02/2710551.html
POJ 3378 树状数组+DP+离散化+高精度相关推荐
- HDU-5542-The Battle of Chibi【树状数组+dp】
HDU-5542-The Battle of Chibi[树状数组+dp] Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/655 ...
- HDU - 5542 The Battle of Chibi(树状数组+DP)
UVA - 12983 The Battle of Chibi(树状数组+DP) HDU - 5542 The Battle of Chibi(树状数组+DP) #include<cstdio& ...
- hdu 6447YJJ's Salesman 离散化+树状数组+DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 因为图中点的坐标值过大,达到1e9.然而只有1e5个点.所以先将其离散化.并按照<x.y& ...
- HDU 2689 POJ 2299 树状数组 + 离散化
以前都是直接树状数组裸奔,昨天一个题数据量好大,过不了,没办法只能学离散化,今天A了第一个离散化的题 #include<stdio.h> #include<string.h> ...
- 树状数组与离散化与求第K大模板
树状数组模板: //Cnt为数组的界 //cnt[i]为编号或者数值上小于id的数量 void Update(LL Id) {while(Id<=Cnt){cnt[Id]++;Id+=lowbi ...
- M元上升子序列【树状数组+dp】
多元组-牛客网 题解 ac代码 #include<iostream> #include<cstring> #include<algorithm> #include& ...
- 【USACO】奶牛抗议 树状数组+dp
题目描述 约翰家的 N 头奶牛正在排队游行抗议.一些奶牛情绪激动,约翰测算下来,排在第 i 位的奶牛 的理智度为 A i ,数字可正可负. 约翰希望奶牛在抗议时保持理性,为此,他打算将这条队伍分割成几 ...
- HDOJ5542-The Battle of Chibi【树状数组,dp】
正题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题目大意 求序列A有多少个长度为M的递增子序列. 解题思路 用fi,jfi,jf_{i,j ...
- CodeForces 314C 树状数组 + dp
//CodeForces 314C //分析:相当于求给定序列的不降子序列的个数,从一个空序列开始将得到的不降子序列不断的延长是典型的做法,则dp[i]表示以第 i 个元素结尾的序列 //思路:O(n ...
- bzoj 2131: 免费的馅饼(树状数组+DP)
2131: 免费的馅饼 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 408 Solved: 245 [Submit][Status][Discu ...
最新文章
- glibc方式安装mysql
- sas和python哪个更容易_我该选择谁?SAS VS Python
- 第一题:Big Countries
- bootstrap 合并菜单_Bootstrap与tab组合,切换菜单实例
- ahjesus解决win下U盘无法写入的问题
- Python进阶(上下文管理器与with语句)
- 手机只能签荣耀!最忠诚代言人胡歌喊你去天猫超品日
- python 函数进度条怎么_刷新你对进度条的认识,用python写出不一样的进度条
- 爬虫-练习引导-豆办电影爬取-json数据的处理
- 数据挖掘竞赛,算法刷题网址汇总
- 论文笔记_S2D.50_Kimera 具有结构规律的增量视觉-惯性三维网格生成
- 移动端H5游戏开发之(移动端尺寸基础知识)
- sola病毒批量恢复工具 —— 大一的回忆
- Less 颜色操作函数Mix的计算方法
- 兜兜转转~~,忘不了的break!! 忘不了的continue!!!!
- Excel成神之道-001-数据分列
- SpringBoot 中连接阿里云rds数据库
- 【数据结构】---堆排序+TOP-K问题(了解游戏排行底层原理)
- linux下也有很多好游戏
- html卡片式ui,十分钟认识UI设计中卡片式设计技法