HDOJ 1025 DP
来源:互联网 发布:淘宝货到付款怎么买 编辑:程序博客网 时间:2024/06/03 19:38
题意:河两边各n座城市,修路,不能交叉,最多几条。
这种题目难还是怎么把模型转换,比如这道题。可以把所要连接的城市当做比较值,然后以初始点当下标。也就是LIS。
比如例子
3
1 2
2 3
3 1
我们就可以当做是长度3的数组a[]={2,3,1};
就是求这个数组的最长子序列了。
#include <iostream>#include <string.h>using namespace std ;const int MAX = 500050;int a[MAX];int dp[MAX];int main(){ int n; while(cin>>n){ memset(dp,0,sizeof(dp)); int u,v; for(int i = 1 ; i <= n ; i++){ cin>>u>>v; a[u]=v; } int t = 0; for(int i = 1 ; i <= n ; i++){ if(dp[t]<a[i]) dp[++t]=a[i]; else{ int l=1,r=t,mid; while(l<r){ mid = (l+r)/2; if(dp[mid]>a[i]) r=mid; else l=mid+1; } } } cout<<t<<endl; } return 0;}
这是我第一次写的代码。超时了。(到这里就回去了,最近两天效率很低。明天要改正啊啊啊啊啊!)
总感觉不应该,放在51NOD就140MS过了。后来才反应过来别人要的不是这个输出格式!!心态小崩。以后千万注意。
#include <iostream>#include <string.h>using namespace std ;const int MAX = 500050;int a[MAX];int dp[MAX];int main(){ ios::sync_with_stdio(false);cin.tie(0); int n; int count=1; while(cin>>n){ memset(dp,0,sizeof(dp)); int u,v; for(int i = 1 ; i <= n ; i++){ cin>>u>>v; a[u]=v; } int t = 0; for(int i = 1 ; i <= n ; i++){ if(dp[t]<a[i]) dp[++t]=a[i]; else{ int l=0,r=t,mid; while(l<r){ mid = (l+r)/2; if(dp[mid]>a[i]) r=mid; else l=mid+1; } mid = (l+r)/2; dp[mid]=a[i]; } } cout<<"Case "<<count++<<":"<<endl; if(t==1) cout<<"My king, at most 1 road can be built."<<endl; else cout<<"My king, at most "<<t<<" roads can be built."<<endl; cout<<endl; } return 0;}
阅读全文
0 0
- HDOJ 1025 DP
- HDOJ 1949 Tourist(DP)
- hdoj 1500 Chopsticks(DP)
- hdoj 1208 dp
- HDOJ Dividing DP
- HDOJ FATE 二维DP
- HDOJ - 2375 状态DP..
- HDOJ - 4502 简单DP..
- HDOJ 1011 树形DP
- HDOJ 1069(经典Dp)
- HDOJ 3944 DP?
- hdoj 3652 数位dp
- 【DP】 HDOJ 2048 4945
- hdoj 4906 状态dp
- hdoj 4960 DP
- 【DP】 HDOJ 4455 Substrings
- HDOJ 4293 dp
- 【DP】 HDOJ 3530 Subsequence
- Ubuntu:搭建以太坊开发环境
- 面向对象中的构造方法以及关键字static的理解
- JS清除HTML之间的空白节点
- mfc绘制曲线
- 从运维角度看中大型网站架构的演变之路
- HDOJ 1025 DP
- Axios——axios在vue中的简单配置与使用
- How to deal with telecom fraud?
- delphi FMX用TImage显示不同格式的图片
- leetcode 204 Count Primes
- QA手册简介目录
- 64位shellcode编程(不错) Windows x64 Shellcode
- MyEclipse设置代码格式化
- LeetCode#554 Brick Wall (week14)