Power oj 1782
来源:互联网 发布:aes算法加密过程 编辑:程序博客网 时间:2024/06/11 07:51
Description
There is n pillars, their heights are (A1,A2,A3,…An. You can jump at the top of the pillars. But you will lose abs(a[j]-a[i])*abs(j-i) power when you jump from i-th pillar to j-th pillar. At first you have m power. Can you jump from s-th pillar to e-th pillar?
Input
The input consists of several test cases.Every test case is two integer n(2<=n<200), q(1=<q<=10000).The second line contain n integer A1, A2, A3,..An.The next q line contain there integer s, e, m .
Output
If you can jump from s to e, with less or equal m power output “Yes”, else output “No”
Sample Input
Raw3 31 2 31 3 21 2 1
Sample Output
RawYesYesNo
Hint
Abs(a-b) mean the absolute value of a-b.
Source
SWUST 7th ACM/ICPC Programming Contest - 2011
///这道题开始以为用dfs搜一遍就可以了,但忽略了这道题是有q组数据,毫无意外的tle,后来在同学的提醒下想到了预处理所有区间,O(1)
查询即可,附上超时代码和ac代码,并以此为鉴。
///超时代码#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<iostream>#define INF 0x7f7f7f7f#define maxn 1005using namespace std;typedef long long LL;int a[210];bool vis[210],flag;void dfs(int s,int e,int m){ if(flag) return ; if(s==e&&m>=0) { flag=true; return ; } for(int i=s+1;i<=e;i++) { dfs(i,e,m-abs(a[i]-a[s])*abs(i-s)); }}int main(){ int n,q,s,e,m; while(~scanf("%d%d",&n,&q)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); while(q--) { flag=false; scanf("%d%d%d",&s,&e,&m); dfs(s,e,m); if(flag) printf("Yes\n"); else printf("No\n"); } } return 0;}///ac代码,动态规划#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<iostream>#define INF 0x7f7f7f7f#define N 205using namespace std;int main(){ int n,q,dp[N][N]; while(scanf("%d%d",&n,&q)!=EOF) { int a[N]; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } for(int i=0;i<n;i++) for(int j=0;j<n;j++) dp[i][j]=abs(a[i]-a[j])*abs(i-j);///dp保存的是从i到j需要的代价 for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(dp[i][k]+dp[k][j]<dp[i][j]) dp[i][j]=dp[i][k]+dp[k][j];///最重要的状态转移,容易想到,代表中间可能经过k点,到达终点 while(q--) { int s,e,m; scanf("%d%d%d",&s,&e,&m); if(dp[s-1][e-1]<=m) printf("Yes\n"); else printf("No\n"); } } return 0;}
0 0
- Power oj 1782
- power oj 2505
- Power OJ 1742试题库问题
- Power OJ 魔术球问题
- UVa OJ 113-Power of Cryptography
- Light OJ 1238 - Power Puff Girls
- 东大OJ-1391-Big big Power
- [LeedCode OJ]#231 Power of Two
- <LeetCode OJ> 326. Power of Three
- [leetcode oj 326]Power of Three
- [leetcode oj 231]Power of Two
- 【P OJ 2406】Power Strings(KMP)
- Power oj 2540 (FFT卷积)
- HDU OJ 4318 Power transmission【最短路spfa】
- <LeetCode OJ> 231 / 342 Power of (Two / Four)
- Leet Code OJ 231. Power of Two [Difficulty: Easy]
- Leet Code OJ 326. Power of Three [Difficulty: Easy]
- Power OJ 2543 赛场布置(网络流最小割)
- 智慧环境
- 九度oj 1335
- C - Valera and Contest题解
- apk文件反编译工具apktool的下载,安装,使用
- Win7环境下用MyEclipse搭建Hbase开发环境
- Power oj 1782
- 黑马程序员--学习OC加强内存管理
- 全排列
- UE4 iOS设备的兼容性
- 利用数组实现缓存实例的不可变类
- 暑假集训第四周阶段二DP A - 最长公共子序列
- JNDI配置c3p0连接池
- hdu-3008魔兽争霸
- [译]Front-end-Developer-Interview-Questions