HDU_1257 最少拦截系统 (dp)
2015-06-23 20:38
281 查看
题目请点我
题解:
这道题是一道很简单的题目,但是比赛的时候卡了很久。发现自己总是在比赛的时候不会快速的去快速建模,卡死到一道题上面。这次比赛最终竟只做出了这一道题,really sucksssss. 练的太少了。。。
这道题其实有很多种方法可以解:
1.暴力
每设置一个标杆从头到尾遍历一次,将所有能消灭的导弹打掉,看最后需要遍历几次;
2.贪心
设置一个标杆数组,每次从头判断使用那个标杆去更新,如果不能更新就新设置一个标杆。最终结果为标杆的数目;
3.dp
最长上升子序列;
测试数据:
5 5 4 3 2 1
0
3 1 2 3
4 1 2 1 2
5 1 2 1 2 1
5 2 1 2 1 2
1 2
1 5
10 1 2 3 4 5 6 7 8 9 10
6 3 5 2 4 1 3
代码实现:
题解:
这道题是一道很简单的题目,但是比赛的时候卡了很久。发现自己总是在比赛的时候不会快速的去快速建模,卡死到一道题上面。这次比赛最终竟只做出了这一道题,really sucksssss. 练的太少了。。。
这道题其实有很多种方法可以解:
1.暴力
每设置一个标杆从头到尾遍历一次,将所有能消灭的导弹打掉,看最后需要遍历几次;
2.贪心
设置一个标杆数组,每次从头判断使用那个标杆去更新,如果不能更新就新设置一个标杆。最终结果为标杆的数目;
3.dp
最长上升子序列;
测试数据:
5 5 4 3 2 1
0
3 1 2 3
4 1 2 1 2
5 1 2 1 2 1
5 2 1 2 1 2
1 2
1 5
10 1 2 3 4 5 6 7 8 9 10
6 3 5 2 4 1 3
代码实现:
1.暴力 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #define INF 30010 #define MAX 100000 #define LL long long using namespace std; LL num; LL result; int lis[MAX]; int main() { //freopen("in.txt","r",stdin); while( scanf("%I64d",&num) != EOF ){ result = 0; int last; bool flag; for( int i = 0; i < num; i++ ){ scanf("%d",&lis[i]); } while( tmp < num ){ last = -1; flag = true; for( int i = 0; i < num; i++ ){ if( lis[i] == -1 ){ continue; } if( lis[i] > last && flag ){ last = lis[i]; lis[i] = -1; tmp ++; flag = false; result ++; } else if( lis[i] > last ){ continue; } else if( lis[i] <= last ){ last = lis[i]; lis[i] = -1; tmp++; } } } printf("%I64d\n",result); } return 0; }
2.贪心 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #define INF 30010 #define MAX 100000 #define LL long long using namespace std; LL num; LL result; int tag[MAX]; int main() { //freopen("in.txt","r",stdin); while( scanf("%I64d",&num) != EOF ){ result = 0; memset(tag,-1,sizeof(tag)); for( LL i = 0; i < num; i++ ){ int tmp; bool flag = false; scanf("%d",&tmp); for( int i = 0 ; i < result; i++ ){ if( tmp <= tag[i] ){ tag[i] = tmp; flag = true; break; } } if( !flag ){ tag[result] = tmp; result++; } } printf("%I64d\n",result); } return 0; }
3.dp #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #define INF 30010 #define MAX 100000 #define LL long long using namespace std; LL num; int result; int dp[MAX]; int lis[MAX]; int main() { //freopen("in.txt","r",stdin); while( scanf("%I64d",&num) != EOF ){ result = 0; memset(dp,0,sizeof(dp)); memset(lis,0,sizeof(lis)); if( num == 0 ){ printf("%d\n",0); continue; } for( int i = 0; i < num; i++ ){ scanf("%d",&lis[i]); } for( int i = 0 ; i < num; i++ ){ dp[i] = 1; for( int j = 0; j < i; j++ ){ if( lis[i] > lis[j] ){ dp[i] = max(dp[i],dp[j]+1); } } result = max(result,dp[i]); } printf("%d\n",result); } return 0; }
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray
- DP Flow
- zoj3605 Find the Marble(三维dp)
- Word Break I,II, Triangle,Palindrome Partitioning 动态规划 DP
- Unique Paths I,II, Minimum Path Sum
- Gas Station