南阳OJ 1204 魔法少女
来源:互联网 发布:上升三法选股公式源码 编辑:程序博客网 时间:2024/05/02 01:35
当我第一次看到这个题时首先想到的是用空间子节点来遍历,然而超时了,然后也就只能老老实实的用动态规划了。
我的第一次代码:
#include<stdio.h>int n,a[10010],s;void F(int m,int h,int t){//m为飞过的层数 if(m+h>n){ if(s>t) s=t; return; } if(m==0){ t+=a[h]; //F(0,h+1,t); F(1,h+1,t); F(2,h+1,t); } else F(0,h+m,t); }int main(){ int i; while(scanf("%d",&n)!=EOF){ s=10000000; for(i=1;i<=n;i++) scanf("%d",&a[i]); F(0,1,0); F(1,1,0); F(2,1,0); printf("%d\n",s); }}
我们可以从题目中知道,少女每上三楼,都必须要走一层楼,所以我们可以利用这个来进行动态规划。
改进后,使用的动态规划:
#include<stdio.h>int a[10005],dp[10005]={0};int min(int b,int c,int d){ int pai[2],i; pai[0]=c; pai[1]=d; for(i=0;i<2;i++) if(b>pai[i]) b=pai[i]; return b;}int main(){ int i,n; while(scanf("%d",&n)!=EOF ){ for(i=1;i<=n;i++) scanf("%d",&a[i]); if(n>3){ dp[0]=0; dp[1]=a[1]; dp[2]=a[2]; for(i=3;i<=n+1;i++) dp[i]=min(dp[i-1],dp[i-2],dp[i-3])+a[i]; printf("%d\n",dp[n+1]); } else if(n==3){ printf("%d\n",min(a[1],a[2],a[3])); } else printf("0\n"); for(i=0;i<=n+1;i++){ a[i]=0; dp[i]=0; } } return 0;}
阅读全文
1 0
- 南阳OJ 1204 魔法少女
- nyist 1204 魔法少女
- nyoj 1204魔法少女
- 魔法少女
- 魔法少女
- 魔法少女
- 魔法少女
- NYOJ 1204 魔法少女 简单DP
- nbut_1010 魔法少女 dp
- 拯救魔法少女
- BZOJ4399: 魔法少女LJJ
- 4399: 魔法少女LJJ
- 【NOJ】[1010] 魔法少女
- nyoj 魔法少女(简单dp)
- nyoj1204 魔法少女 线性DP
- BZOJ4399: 魔法少女LJJ treap
- BZOJ 4399: 魔法少女LJJ
- 南阳OJ
- 酒店OTT小区IPTV完整解决方案
- 'React/RCTBundleURLProvider.h’ file not found
- YUV转RGB(NV21-ARGB)的Neon优化代码
- iOS--UIWebView设置backgroundColor无效问题
- Python陷阱
- 南阳OJ 1204 魔法少女
- 移植FreeRTOS一定要修改startup_stm32f10x_hd.s的启动文件吗?
- Java中类加载机制和反射技术
- strtok小记
- java HttpGet HttpPost区别
- HDU3038 How Many Answers Are Wrong (加权并查集)
- Android的按钮Button单击事件及监听器的实现方式
- jQuery操作checkbox选择
- JSP中的编码设置