弱校联萌 BNU I. Increasing or Decreasing
来源:互联网 发布:知乎商业模式 编辑:程序博客网 时间:2024/06/05 17:36
【题意】求L,R区间里面严格递增或者严格递减的数的个数。
【解题方法】数位DP。正解给了两种方法,我直接用数位DP水过去了。
【代码君】
////Created by just_sort 2016/10/6//Copyright (c) 2016 just_sort.All Rights Reserved//#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;int digit[20];LL dp[20][20][5];LL dfs(int l,int pre,int flag,bool jud){ if(l == 0) return 1; if(!jud && dp[l][pre][flag]!=-1) return dp[l][pre][flag]; int nex = jud?digit[l]:9; LL ret = 0; for(int i = 0; i <= nex; i++){ int news; if(flag==1&&i==0) news = 1; else if(flag==1||(flag==2&&pre==i)) news = 2; else if(flag==2) news = pre>i?3:4; else{ if(flag==3&&i>pre) continue; if(flag==4&&i<pre) continue; else news = flag; } ret += dfs(l-1,i,news,jud&&(i==nex)); } if(!jud) dp[l][pre][flag]=ret; return ret;}LL f(LL num){ int pos = 0; while(num){ digit[++pos] = num%10; num /= 10; } return dfs(pos,0,1,1);}int main(){ memset(dp,-1,sizeof(dp)); int T; scanf("%d",&T); while(T--) { LL l,r; scanf("%lld%lld",&l,&r); printf("%lld\n",f(r)-f(l-1)); } return 0;}
0 0
- 弱校联萌 BNU I. Increasing or Decreasing
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing bnu 52325
- vim:Increasing or decreasing numbers
- 数位DP Increasing or Decreasing
- 2016弱校联盟十一专场10.5 I Increasing or Decreasing(数位dp)
- BNUOJ52325 Increasing or Decreasing(数位dp)
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing
- Sort Stack in increasing/decreasing Order.
- Leetcode665 (Array) Non-decreasing Array +Leetcode674 Longest Continuous Increasing Subsequence
- BNU 0817 A 0 or 1
- BNU 0814 I. Pakhom and the Gully
- BNU
- BNU 4260 Trick or Treat && ZOJ 3386 (三分查找)
- BNU 2012 Summer Selection Contest I D题 Olympics
- HNUST二队练习赛BNU I、编码
- I - 0 or 1
- Decreasing String
- Which is fast : ++i or i++?
- Vijos P1911 珠心算测验
- 剑指Offer面试题13(java版):在O(1)时间删除链表节点
- 16.10.6 C组总结
- 验证码的生成
- was cached in the local repository, resolution will not be reattempted until(Maven常见问题)
- 弱校联萌 BNU I. Increasing or Decreasing
- win32快速实现listctrl控件的可编辑子项
- spring struts1 管理action
- 美团2017笔试题 给定一颗多叉树
- P1315 观光公交
- clusterinit初始化环境脚本
- ccpc东北四——重现赛
- 161006
- Codeforces722C-Destroying Array(线段树 or 并查集)