BNUOJ52325 Increasing or Decreasing(数位dp)
来源:互联网 发布:windows代表什么 编辑:程序博客网 时间:2024/06/05 11:54
题意:给你一个区间,让你求区间里面数字的每一位数字都不递增或不递减的数的个数,比如1234,55543,432,11,4这些,求这些数的个数。
思路:数位dp,用dp[i][j][0or1]表示i位数字以j开头,0表示下降,1表示上升的满足题意的数的个数。用fu[i][j]表示i位以j开头的上升和下降重复的数的个数。要注意处理下降的数的时候得把以0开头的特殊记,否则转移方程写起来会更麻烦= =(因为我写的本来就比较麻烦T^T),后面的dp部分得仔细思考,然后其实没啥难的,主要是思路得清晰= =!
//#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include<string.h>#include<vector>#include<queue>#include<algorithm>#include<stdio.h>#include<math.h>#include<map>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<deque>using namespace std;typedef long long ll;ll dp[25][10][2],fu[25][10];///0递增1递减void init(){ memset(dp,0,sizeof(dp)); memset(fu,0,sizeof(fu)); for(int i=0; i<10; i++) dp[1][i][0]=dp[1][i][1]=fu[1][i]=1; for(int i=2; i<25; i++) for(int j=0; j<10; j++) for(int k=0; k<10; k++) if(j<=k) dp[i][j][0]+=dp[i-1][k][0]; for(int i=2; i<25; i++) for(int j=0; j<10; j++) for(int k=0; k<10; k++) if(!j) dp[i][j][1]+=dp[i-1][k][1]; else if(j>=k) { if(k) dp[i][j][1]+=dp[i-1][k][1]; else dp[i][j][1]++; } for(int i=2; i<25; i++) for(int j=0; j<10; j++) for(int k=0; k<10; k++) if(!j) fu[i][j]+=fu[i-1][k]; else fu[i][j]=1; /*for(int i=1;i<=5;i++) for(int j=0;j<10;j++) cout<<"fu["<<i<<"]["<<j<<"]="<<fu[i][j]<<endl; */ /*for(int j=1; j<=4; j++) for(int i=0; i<10; i++) cout<<"dp["<<j<<"]["<<i<<"][1]="<<dp[j][i][1]<<endl; */}ll get(ll x){ if(!x) return 1; else if(x>0&&x<10) return (ll)(x+1); int digit[25],bj=0; while(x) { digit[bj++]=x%10; x/=10; } //for(int i=0; i<bj; i++) // cout<<digit[i]<<endl; ll ans=0; for(int i=bj-1; i>=0; i--) { int flag=0; for(int j=bj-2; j>i; j--) if(!flag&&digit[j+1]>digit[j]) flag=1; else if(!flag&&digit[j+1]<digit[j]) flag=2; else if(flag==1&&digit[j+1]<digit[j]) { flag=-1; break; } else if(flag==2&&digit[j+1]>digit[j]) { flag=-1; break; } if(flag==-1) break; int l=digit[i]; if(!i)l++; //cout<<"i="<<i<<"-->l="<<l<<endl; for(int j=0; j<l; j++) { //cout<<"j="<<j<<",flag="<<flag<<":"; if(!flag) { ///ans+=(dp[i+1][j][0]+dp[i+1][j][1]-fu[i+1][j]); if(i==bj-1||j==digit[i+1]) ans+=(dp[i+1][j][0]+dp[i+1][j][1]-fu[i+1][j]);//cout<<"dp["<<i+1<<"]["<<j<<"][0]+dp["<<i+1<<"]["<<j<<"][1]="<<dp[i+1][j][0]<<"+"<<dp[i+1][j][1]<<"-"<<fu[i+1][j]<<endl; else if(j>digit[i+1]) ans+=dp[i+1][j][0];//cout<<"dp["<<i+1<<"]["<<j<<"][0]="<<dp[i+1][j][0]<<endl; else { if(j) ans+=dp[i+1][j][1];//cout<<"dp["<<i+1<<"]["<<j<<"][1]="<<dp[i+1][j][1]<<endl; else ans++;//cout<<"ans++"<<endl; } } else if(flag==1) { if(j<=digit[i+1]) { if(j) ans+=dp[i+1][j][1];//cout<<"dp["<<i+1<<"]["<<j<<"][1]="<<dp[i+1][j][1]<<endl; else ans++;//cout<<"dp["<<i+1<<"]["<<j<<"][1]="<<1<<endl; } } else if(flag==2) { if(j>=digit[i+1]) ans+=dp[i+1][j][0];//cout<<"dp["<<i+1<<"]["<<j<<"][0]="<<dp[i+1][j][0]<<endl; } } //cout<<ans<<endl; } //cout<<"*"<<ans<<endl; return ans;}int main(){ init(); int t; scanf("%d",&t); while(t--) { ll l,r; scanf("%lld%lld",&l,&r); ll ans1=get(l-1),ans2=get(r),ans=ans2-ans1; printf("%d\n",ans); } return 0;}
0 0
- BNUOJ52325 Increasing or Decreasing(数位dp)
- 数位DP Increasing or Decreasing
- 2016弱校联盟十一专场10.5 I Increasing or Decreasing(数位dp)
- vim:Increasing or decreasing numbers
- 弱校联萌 BNU I. Increasing or Decreasing
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing bnu 52325
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing
- Sort Stack in increasing/decreasing Order.
- zoj 3162 To Go or Not to Go 数位DP
- UESTC 1307 数位DP (递归 or 非递归)
- poj 3252 Round Numbers 数位DP or 组合数学
- LightOJ 1352 Strange Summation(找规律or数位DP)
- HDU 2089 暴力or两种思路的数位DP
- 17浙江省赛 数位dp or xjb乱搞
- Leetcode665 (Array) Non-decreasing Array +Leetcode674 Longest Continuous Increasing Subsequence
- 数位dp
- 数位DP
- 数位DP
- 记录几次内存泄露问题
- maven学习2-建立Java工程
- 在Ubunut12.04上搭建TFTP和NFS服务器
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- 1006-Tick and Tick
- BNUOJ52325 Increasing or Decreasing(数位dp)
- vs2010 瘦身--ipch文件夹和sdf文件配置
- Ubuntu 12.04 安装 GitLab
- poj 1426 Find The Multiple(DFS)
- bnuoj 52317 As Easy As Possible(预处理+倍增法)
- SDUT OJ 数据结构实验之栈四:括号匹配
- python爬虫——模拟登陆教务系统
- java中String类常量池详解
- Leetcode-58. Length of Last Word