08 2021 档案
摘要:家庭问题 图似乎早早地来了!┭┮﹏┭┮ 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int N=105; 6 int n,k,ans,maxx,vis[N],a[N]
阅读全文
摘要:产生数 “位变换” 存在数组 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int N=10005; 6 int n,k,r[20][2],exist[N],ans=1
阅读全文
摘要:奇怪的电梯 注意起点等于终点的情况! 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int N=205; 6 int n,a[N],b[N]; 7 queue<int>
阅读全文
摘要:围成面积 技巧:将整个二维数组再围一圈! 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int N=12; 6 int ans,a[N][N],t[]={-1,1,0,
阅读全文
摘要:连通块 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int N=105; 6 int n,m,ans,a[N][N],t[]={-1,1,0,0,0,0,-1,1};
阅读全文
摘要:围圈报数 区分什么时候将队首元素放入队尾,什么时候将队首元素直接丢弃。 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 6 queue<int> q; 7 8 int main(){
阅读全文
摘要:Blah数集 如果用队列不能只求前n个数,因为第n+1个数可能小于第n个数。 那么只对求出的前n个数进行排序然后取第n个数显然不是正确答案。 所以用队列复杂度很高,使用数组结合题意来写最简单。 1 #include<iostream> 2 #include<cstdio> 3 #include<al
阅读全文
摘要:周末舞会 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 queue<int> q1,q2; 6 void init(bool f,int m){ 7 for(int i=1;i<=
阅读全文
摘要:准备两个栈: 一个操作数栈,一个运算符栈。 1.遇到操作数直接压栈(操作数栈) 2.遇到左括号直接压栈(运算符栈) 3.遇到基本运算符分两种情况讨论: 3.1.运算符栈栈顶元素优先级大于等于当前运算符优先级或者栈顶元素为左括号——直接压栈(运算符栈) 3.2.运算符栈栈顶元素优先级小于当前运算符优先
阅读全文
摘要:车厢调度 将入口A和车站C分别视为一个栈,其中A栈可以弹出到B或C,C只能弹出到B。 1 #include<iostream> 2 #include<cstdio> 3 #include<stack> 4 using namespace std; 5 6 const int N=1005; 7 st
阅读全文
摘要:字符串匹配问题 使用全局stack时,切记清空stack! 1 #include<iostream> 2 #include<cstring> 3 #include<stack> 4 using namespace std; 5 6 stack<int> sta; 7 8 bool match(){
阅读全文
摘要:括弧匹配检验 1 #include<iostream> 2 #include<cstring> 3 #include<stack> 4 using namespace std; 5 6 stack<char> sta; 7 8 bool match(){ 9 string s; 10 cin>>s;
阅读全文
摘要:表达式括号匹配 注意考虑出现多余右括号的情况! 1 #include<iostream> 2 #include<cstring> 3 #include<stack> 4 using namespace std; 5 6 stack<char> sta; 7 8 bool match(){ 9 cha
阅读全文
摘要:后缀表达式的值 1 #include<iostream> 2 #include<cstring> 3 #include<stack> 4 using namespace std; 5 typedef long long ll; 6 ll t; 7 stack<ll> sta; 8 ll cal(ll
阅读全文
摘要:鸡蛋的硬度 简单的解释在代码里有进行说明。 具体的题解可参考 大佬 。 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const int N=105; 5 int f[N][N]; 6 7 int main()
阅读全文
摘要:糖果 解题思路可参考 判断整除。 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 const int N=105; 6 int f[N][N]; 7 8 int main()
阅读全文
摘要:Maximum sum 和 股票买卖 差不多。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=50005;
阅读全文
摘要:数的划分 与 鸣人的影分身 不能说相似吧,只能说一模一样。 深搜应该也没问题,这里就不尝试了。 下面贴一个dp解法(上一题的注释懒得改了⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄) 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4
阅读全文
摘要:鸣人的影分身 数据不大,深搜也能过得去。 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int ans,k; 5 void dfs(int b,int s,int d){ 6 if(d==k){ 7 if(!b)a
阅读全文
摘要:股票买卖 和 合唱队形 有异曲同工之妙! 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=1e5+5; 7 i
阅读全文
摘要:大盗阿福 别问我为什么突然用scanf了,因为cin过不了 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1e5+5; int a[
阅读全文
摘要:计算字符串距离 考虑一种特殊情况“sad adf” 最初我仅将f[0][0]初始化为0,程序得到的答案一直是3,但可以看出正确的答案应当是2。 后面发现应当将数组边缘进行正确的初始化。 1 #include<iostream> 2 #include<cstring> 3 #include<algor
阅读全文
摘要:公共子序列 基本思想与 编辑距离 一致。 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=205; 6 int f[N][N]; 7 8 int m
阅读全文
摘要:橱窗布置 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=105; 7 int a[N][N],f[N][N]
阅读全文
摘要:滑雪 深搜复杂度还是高了点。 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 const int N=105; 6 int r,c,maxx,a[N][N],t[]={-1,
阅读全文
摘要:复制书稿 下面这个只能拿70分。 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int N=505; 7 int a[N],f[N][N],t[N][N]
阅读全文
摘要:方格取数 用带贪心的dp只能拿八十分! 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int N=15; 7 int n,r,c,a[N][N]; 8 i
阅读全文
摘要:编辑距离 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int N=2005; 7 string a,b; 8 int ans[N][N]; 9 int
阅读全文
摘要:乘积最大 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 typedef long long ll; 5 const int N=10; 6 ll s,f[N][N],a[N][N]; 7 int main(){
阅读全文
摘要:合并石子 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int N=105; 8 int a[N],f[N][N];
阅读全文
摘要:宠物小精灵之收服 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int N=105; 8 int a[N],b[N]
阅读全文
摘要:开餐馆 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int N=105; 8 int a[N],b[N],f[N]
阅读全文
摘要:Charm Bracelet 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 6 const int N=3505; 7 int a[N],b[N],f[12885]; 8
阅读全文
摘要:货币系统 1 #include<iostream> 2 using namespace std; 3 4 const int N=1005; 5 int a[N]; 6 long long f[N][N*10]; 7 int main(){ 8 int n,m; 9 cin>>n>>m; 10 fo
阅读全文
摘要:潜水员 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int N=1005; 7 int a[N],b[N],c[N],f[105][105]; 8 in
阅读全文
摘要:分组背包 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N=205; 6 struct knapsack{ 7 int w,c,p; 8 }; 9 knapsack ns[N]; 10
阅读全文
摘要:混合背包 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N=205; 6 int a[N],b[N],c[N],f[N][N]; 7 int main(){ 8 int m,n; 9 c
阅读全文
摘要:庆功会 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N=505; 6 int a[N],b[N],c[N],f[N][6005]; 7 int main(){ 8 int m,n; 9
阅读全文
摘要:完全背包问题 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N=35; 6 struct knapsack{ 7 int w,v; 8 }; 9 knapsack ns[N]; 10 i
阅读全文
摘要:背包问题 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N=205; 6 int a[N],b[N],f[N][N]; 7 int main(){ 8 int m,n; 9 cin>>m
阅读全文
摘要:最大子矩阵 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N=105; 6 int a[N][N]; 7 int main(){ 8 int n,maxx=-1000000000; 9
阅读全文
摘要:怪盗基德的滑翔翼 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=105; 5 6 int a[N],b[N],c[N]; 7 int main(){ 8 int t,n,maxx; 9 cin
阅读全文
摘要:摘花生 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=105; 5 6 int a[N][N],f[N][N]; 7 int main(){ 8 int t,r,c,maxx=0; 9 cin
阅读全文
摘要:登山 #include<iostream> #include<cstdio> using namespace std; const int N=1005; int a[N],b[N],c[N]; int main(){ int n,t,maxx=0; cin>>n; for(int i=1;i<=n
阅读全文
摘要:最长上升子序列 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=1005; 5 6 int a[N],f[N]; 7 int main(){ 8 int n,maxx=0; 9 cin>>n;
阅读全文
摘要:机器分配 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=25; 5 6 int a[N][N],b[N][N],f[N][N]; 7 void show(int r,int c){ 8 if(
阅读全文
摘要:最长公共子序列 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=1005; 5 6 string x,y; 7 int a[N][N]; 8 int main(){ 9 cin>>x>>y; 1
阅读全文
摘要:合唱队形 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=105; 5 6 int a[N],b[N],c[N]; 7 int main(){ 8 int n,t,maxx=0; 9 cin>>
阅读全文
摘要:友好城市 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 const int N=5005; 6 7 struct sn{ 8 int s,n; 9 }; 10 sn r[N
阅读全文
摘要:挖地雷 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=205; 5 int a[N][N],c[N],f[N]; 6 int main(){ 7 int n,x,y,t,r,maxx=0; 8
阅读全文
摘要:城市交通路网 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=105; 5 int main(){ 6 int n,t=1,a[N][N],c[N],f[N]; 7 cin>>n; 8 for(
阅读全文
摘要:拦截导弹 1 #include<iostream> 2 using namespace std; 3 const int N=1005; 4 int main(){ 5 int t,r=0,cnt=0,maxx=0,k[N],a[N],b[N]; 6 while(cin>>t){ 7 //第一问:d
阅读全文
摘要:求最长不下降序列 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=205; 5 int a[N],b[N]; 6 int main(){ 7 int n,t,maxx=0; 8 cin>>n;
阅读全文
摘要:数字金字塔 第一道dp,纪念一下!!! 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=1005; 5 int b[N][N]; 6 int main(){ 7 int n,t; 8 cin>>
阅读全文
摘要:Knight Moves 思路和 最少步数 一致。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 7 const int N=305; 8
阅读全文
摘要:献给阿尔吉侬的花束 思路和 Dungeon Master 一致。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 7 const int N=
阅读全文
摘要:迷宫问题 思路肯定是可以参考 Dungeon Master 的。 但是在此基础上起点和终点已经确定,另外迷宫的表示也是数值。 我这里打印结果用的是深搜(肯定还有其他方法) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #i
阅读全文
摘要:抓住那头牛 这题居然是一维的! 但是笔者窃以为难度并没有比二维的低。 由于N的取值比较大(100000),为避免全数轴确定最短路径(数值), 这里加了一个限制条件 !a[k]||a[y]<a[k] 。 这样就能有效避免多余的求值。 1 #include<iostream> 2 #include<cs
阅读全文
摘要:走迷宫 思路和 Dungeon Master 一致。 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 6 const int N=45; 7 char a[N][N]; 8 int
阅读全文
摘要:仙岛求药 思路和 Dungeon Master 一致。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 7 const int N=25; 8
阅读全文
摘要:The Castle 首先,这题大体上肯定是可以参考 细胞 的。 另外, 关于最大房间的求解,只要对每一个队列求一次房间数,取最大值即可! 由于每个房间的“墙”使用了二进制表示(实际上是对应二进制的十进制值), 这里我反其道而行之,将这个十进制值又转化为二进制(实际上也是对应二进制的十进制值(表示方
阅读全文
摘要:Lake Counting 和 细胞 相比,不能说相似,只能说一模一样!(*╹▽╹*) 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 6 const int N=1005; 7 i
阅读全文
摘要:Dungeon Master 这个相当于在 最少步数 的基础上增加了一些约束条件。 也就是不需要给所有点确定最短路径,只需要在有意义的点上确定就行。 因为最短路径(数值)和迷宫(字符)使用了两种不同的表示方式,所以最好使用两个数组搭配求解。 1 #include<iostream> 2 #inclu
阅读全文
摘要:最少步数 这个和 细胞 不同,不需要对一整块进行标记,而是给每一个点确定一个最短路径(仅数值)。 1 #include<iostream> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 const int N=100;
阅读全文
摘要:细胞 做的第一道广搜题,纪念一下!!! 题解: main函数中的循环每次进入bfs()函数,会将“隶属于一个细胞”的所有细胞进行搜索并标记。 由于下一个细胞群的位置无法确定,所以只能老老实实遍历了,但由于有标记,不会重复多余的进入bfs()函数。 相当于main函数中的循环进入几次bfs()函数,就
阅读全文
摘要:一元三次方程求解 原以为这里的分治体现在分化区间,没想到重心在二分法的分治思想上!┭┮﹏┭┮ 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 double a,b,c,d; 6 double fun(double
阅读全文
摘要:求排列的逆序数 题解可参考 归并排序。 严肃声明:并没有水博客,归并排序是归并排序,分治是分治! 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int N=1e5+5; 6 int a[N]; 7
阅读全文
摘要:区间合并 因为题目明确说了a,b∈[1,10000],此时使用数组的状态来判断某个值是否在区间内,无疑是可行的。 但是如果题目的测试案例给的再恶心点(如50000个1,10000),复杂度应该就过不了。┭┮﹏┭┮ 看到很多大佬也都是先对数组进行排序,遍历所有区间然后判断是否满足合并区间条件。 很可惜
阅读全文
摘要:记得曾参考教材与几篇博客,最终写出自己的快速排序终极版。 详情可见 排序系列。 最终的版本大概是下面这个样子。 1 int Partition(int first, int end) { 2 //此处选择数列第一个记录作为基准 3 int tmp = r[first]; 4 while (first
阅读全文
摘要:2011 假定三个2011相乘,取出前两个2011相乘的结果后四位与第三个2011相乘并对结果取后四位即为最终结果。 看到这是不是就想循环求解? 让我来滋醒你,200位的循环次数,不爆是不可能的! 而使用分治,200位大概等于2700,也就是说最多700轮分治左右(这里不算特殊情况20111)。 所
阅读全文
摘要:输出前k大的数 写这题时还偶然发现以前写的优化快排居然是错误的。——》快速排序优化方案的否定 思路的话,大致就是快排的删减版,即减少不必要的分治。(25行) 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 cons
阅读全文
摘要:黑白棋子的移动 当左边连续白棋还有一定数量时(>4),每次移动其实是一样的方式。(16、23行) 当按照这种固定的方式进行移动后,除去移动好了的棋子整个棋盘可以看作是原棋盘的子集。 至于左边白棋数量等于4时,就可以当做临界条件特殊处理。(16、18、19、20、21行) 1 #include<ios
阅读全文
摘要:循环比赛日程表 当m=3时 第一天 第二天 第三天 第四天 第五天 第六天 第七天 1 2 3 4 5 6 7 8 2 1 4 3 6 5 8 7 3 4 1 2 7 8 5 6 4 3 2 1 8 7 6 5 5 6 7 8 1 2 3 4 6 5 8 7 2 1 4 3 7 8 5 6 3 4
阅读全文
摘要:最大子矩阵 用的方法应该叫并查集。 更恰当的题目应当是相邻最大子矩阵。 1 #include<iostream> 2 using namespace std; 3 4 const int N=105; 5 int a[N][N]; 6 int main(){ 7 int n,maxx=-100000
阅读全文
摘要:An Easy Problem 实话实说我也不知道为什么它归类在贪心算法里。(*❦ω❦) 1 #include<iostream> 2 using namespace std; 3 4 int cnt(int n){ 5 int sum=0; 6 while(n){ 7 if(n%2)sum++;
阅读全文
摘要:拦截导弹问题 易错点在于: 不考虑“之前”的导弹拦截系统。 每次“无奈”增加导弹拦截系统后,之前的导弹拦截系统也是有效的,也是能够发挥作用的。 1 #include<iostream> 2 using namespace std; 3 const int N=1005; 4 int main(){
阅读全文
摘要:删数问题 这里面有一个未明确说明的情况: 答案的第一位可以为0,甚至答案的每一位都可以为0。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int N=245; 6
阅读全文
摘要:均分纸牌 遍历纸牌多退少补就完事了!(*^▽^*) 如果已经有纸牌堆满足要求,此时无需移动纸牌! 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=105; 5 int t[N]; 6 int m
阅读全文
摘要:放苹果 之所以要约束下次分配苹果的最小个数,是因为如果不进行约束,就有可能计算重复情况。 初始为1也是基于同样的考虑。 1 #include<iostream> 2 using namespace std; 3 int k,n; 4 //c表示下次分配最小个数,d表示分配到了第几个盘子,b表示还有多
阅读全文
摘要:马走日 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int a[10][10],r[16],n,m,cnt; 6 7 void dfs(int x,int y,int d){ 8 if(d==n*m){ 9
阅读全文
摘要:取石子游戏 1 #include<iostream> 2 using namespace std; 3 4 int main(){ 5 int a,b; 6 while(cin>>a>>b){ 7 if(!a&&!b)break; 8 int ans=0; 9 int t=max(a,b); 10
阅读全文
摘要:棋盘问题 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int N=10; 6 int ans,n,k,a[N][N],b[N]; 7 8 bool check(int c,int r){ 9 fo
阅读全文
摘要:红与黑 没错,与 迷宫 一模一样! ̄□ ̄|| 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int N=25; 6 int w,h,ans,a[N][N],che[8]; 7 bool check(
阅读全文
摘要:迷宫 因为不需要找最优路径,只需要得到结果即可(YES/NO),所以走过的位置不需要还原。 显然,使用这种方式大大减少了运行时间(降低了时间复杂度)。 下面验证其合理性: 每一点只要可走就可以重复走、来回走(当然程序内部肯定不这样做),换言之,任何相交路径中的任意两点都是相通的。 到达任一点可能有多
阅读全文
摘要:八皇后 首先通过 八皇后问题 求解出所有的结果,然后将每一个结果转化为一个八位的整数(算是本题的一个亮点吧) 将这些八位的整数集进行排序,然后依据输入输出相应的答案。 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 us
阅读全文
摘要:八皇后问题 首先可以试图去简化问题,将问题转化为为每一列确定一个有效的行号。 因为同一列只能有一个皇后,并且需要在八列中确定八个皇后,即每一列都必定有且只有一个皇后。 经过简化后,显然,通过一个一维数组即可以确定一组有效解。 关于check: 不为同一行或同一列的判定比较简单(这里省略) (i1,j
阅读全文
摘要:LETTERS 由于是找一条最优路径(最多可以经过几个字母),所以在深度遍历回来的时候要注意还原。 否则最优路径会受到其他路径的干扰! 另外关于check这里写的过于冗余,可以通过构造数组来改善。 1 #include<iostream> 2 #include<cstdio> 3 using nam
阅读全文
摘要:自然数的拆分 与 分解因数 、 组合的输出 不能说相似,只能说是一模一样。 大致思路参考以上两篇。 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 const int N=105; 6 int n,a[N]; 7
阅读全文
摘要:组合的输出 与 分解因数 类似,为递归函数再指定一个形参,约束循环的起始条件 每满足一次临界条件,就输出一次结果。 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 const int N=25; 6 int n,r
阅读全文
摘要:集合的划分 想的时候,这题怎么这么难!!! 看了题解后,题解好牛!!! 这大概就是废物日常吧! ̄□ ̄|| 以下均摘抄至官方解释: 考虑一般情况,对于任意的含有n个元素a1,a2,......,an的集合S,放入k个无标号的盒子中去,划分数为S(n,k),我们很难凭直觉和经验计算划分数和枚举划分的所有
阅读全文
摘要:分解因数 这题很明显与 数的计数 有异曲同工之处。 但如果用它的方法来解这题却是行不通的。 试分析一种情况,设m=20; 那么共有四种情况,分别为2*10,2*2*5,4*5,20; 但如果用上一种方法来做的话,却有2*10,2*2*5,4*5,2*2*5,20共五种情况。 究其原因是因为左边因式经
阅读全文
摘要:括号匹配问题 做的时候脑袋虽然晕乎乎的,但心里就是想用一个函数借助递归思想完成该题(不借助主函数) 当然最终结果看似是达到这个要求了(*^▽^*)。 关于这种解法的解释就不说了,毕竟没有实用性! 1 #include<iostream> 2 #include<cstring> 3 using nam
阅读全文
摘要:逆波兰表达式 非常标准的一个递归题 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 using namespace std; 5 6 const int N=55; 7 char a[N]; 8 double antiPol
阅读全文
摘要:数的计数 看到题目,我们很容易就会写出类似于下面这样的代码。 然后最后一个测试集过不去……(ε=(´ο`*)))唉) 1 #include<iostream> 2 using namespace std; 3 4 int ans; 5 void cnt(int ori){ 6 ans++; 7 fo
阅读全文
摘要:山区建小学 该题的难点在于存在两个“子动态规划”并且需要用一个中间数组将两个“子动态规划”联系起来。 解题思路: 计算任意两个村子之间的距离,用动态规范显然降低了不少复杂度。(都可以单独出一个题目了~~(╯﹏╰)~~) 在任意两个村子之间建一所小学,并计算两个村子之间的所有村子到该小学的最短距离,求
阅读全文
摘要:踩方格 做了好几个递推题,大多是披着递推的动态规划o(╥﹏╥)o 相对来说这题算是比较正经的递推了(*^▽^*) 只要区分开上一轮是从东/西边过来的还是从南边过来的,这题就基本ok了。 如果是从东(西)边过来的,那么这一步只能往西(东)走或者往北走。 如果是从南边过来的,那么这一步可以往东/西/北三
阅读全文
摘要:判断整除 将前n-1个数看成一个整体序列,则n个数可能产生的结果就等于“前n-1个数产生的所有结果分别加上第n个数或分别减去第n个数” n-2也同理……直到1为止。 由于是判断有无结果能被k整除,所以所有的中间结果都可以取余k,从而使所有可能的结果保持在一个不大的范围,用数组进行存储。 下面代码中的
阅读全文
摘要:流感传染 做这题时,程序最容易犯的错误就是认为“今天刚染病的人也具有传染性” 显然,今天刚染病的人昨天是不具有传染性的,所以今天染病的人绝不是因这种刚染病的人引起的! 所以在检查健康人今天是否存在染病风险时,应避免这一误区! 我这里使用的方法是对病人染病日期进行记录。 1 #include<iost
阅读全文
摘要:斐波那契数列 代码当然是非常简单的。 最令我满意的是核心循环的“循环头”。 它使得程序尽最大努力不做不必要操作。 当然,它也恰如其分的满足了这种多组输入输出的需要。 1 #include<iostream> 2 using namespace std; 3 4 const int N=1e6+5;
阅读全文
摘要:过河卒 下面是常规的代码,过当然是不能过的。 之所以放上来是因为它好歹也是个看的过去的常规搜索递归程序(可惜没找到改成尾递归的方法o(╥﹏╥)o) 1 #include<iostream> 2 using namespace std; 3 int n,m,ans,x[9],y[9]; 4 5 boo
阅读全文
摘要:位数问题 递推写着写着,然后,动态规划的味道越来越浓了! 1 #include<iostream> 2 using namespace std; 3 const int N=1005; 4 int main(){ 5 int n,f[N][2]; 6 cin>>n; 7 f[1][0]=9,f[1]
阅读全文
摘要:昆虫繁殖 下面的代码是标准答案,但笔者窃以为它“并不正确”! 举个例子来说吧 令x=3,y=1,z=9 第x天 1 2 3 4 5 6 7 8 9 成虫数 1 1 1 1 1 2 2 2 3 卵虫数 0 0 0 1 1 0 1 1 (2)1 推导后可以知道第九天卵虫数为1对,但如果按照下面代码中的算
阅读全文
摘要:对基本的简单选择排序进行了优化。 详情可参考 排序系列 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 const int N=105; 6 7 int main(){ 8 int n,k,num[N]; 9 do
阅读全文
摘要:谁考了第k名 注意: 需要稳定排序算法 下面的代码窃以为更适合这题,但是不能过。 因为快速排序并不是稳定的算法。 21.8.11更新:错误的真正原因:快速排序优化方案的否定。 1 #include<iostream> 2 #include<cstdio> 3 using namespace std;
阅读全文
摘要:明明的随机数 其实质就是将数组下标作为“桶”的标识 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int N=1005; 6 7 int main(){ 8 int a[N],v,cnt=0; 9
阅读全文
摘要:求逆序对 本着别人听懂了自己才算会的原则,今天就在这里丢人现眼一回(灬ꈍ ꈍ灬)。 题意要求找出所有的逆序对。 首先就要明确两点: 仅仅对其进行排序然后记录执行次数是于事无补的。 暴力数的话时间复杂度是一定通不过的。 解决方案: 将注意力聚焦到归并排序上(也可参考 排序系列 )。 归并排序的特点==
阅读全文
摘要:车厢重组 对基本冒泡排序进行了优化(具体体现在exchange、bound) 详情可参考我的一篇csdn博客--》排序系列 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 const int N=10005; 6
阅读全文
摘要:回文数 思路还是很清晰的: 接收输入 构造相加(倒序相加)(个人是按照大整数加法的规格来的) 对结果判断是否为回文数(单独写一个函数进行回文判断isPalindrome) 循环二、三步 打印输出 这里重点说几个注意点(本人犯的错,拿出来给大伙乐呵乐呵): 输入可以是16进制,即输入有可能含有正确的字
阅读全文
摘要:阶乘和 这题本来应该很容易AC的,没想到提交了好几次。 基本思路可以参考 求10000以内n的阶乘。 我发现常量N如果不到10000!的量级,就会报运行时错误(指数组溢出等)。 也就是说很可能它拿你这个做了10000的阶乘和。 但是题目里明明写着n<=50,o(╥﹏╥)o!(而且这个程序也没有做10
阅读全文
摘要:求10000以内n的阶乘 第一次把“carry”与“各位相乘”两个过程进行了分离,然后时间复杂度超了…… 想不到合在一起居然可以过! (理论上两种方式的复杂度只是隔了2倍而已,Ծ‸Ծ) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstrin
阅读全文

浙公网安备 33010602011771号