https://vjudge.net/contest/161167#problem/F——dp
来源:互联网 发布:面试算法书籍推荐 知乎 编辑:程序博客网 时间:2024/05/17 04:00
Think:
1求非严格单调递增子序列和非严格单调递减子序列
2反思:
1>lower_bound()—-Wrong Answer
upper_bound()—-Accepted
建议参考博客
以下为Wrong Answer代码——lower_bound()可行数据覆盖?
#include <cstdio>#include <algorithm>using namespace std;int n, a[100004], b[100004];int dp1();int dp2();int main(){ int T, i; scanf("%d", &T); while(T--){ scanf("%d", &n); for(i = 1; i <= n; i++) scanf("%d", &a[i]); int x1 = dp1(); int x2 = dp2(); if(x1 == n-1 || x1 == n || x2 == n-1 || x2 == n) printf("YES\n"); else printf("NO\n"); } return 0;}int dp1(){ int i, len, pos; b[1] = a[1]; len = 1; for(i = 2; i <= n; i++){ if(a[i] >= b[len]){ len += 1; b[len] = a[i]; } else { pos = lower_bound(b+1, b+1+len, a[i]) - b; b[pos] = a[i]; } } return len;}int dp2(){ int i, len, pos; b[1] = a[n]; len = 1; for(i = n-1; i >= 1; i--){ if(a[i] >= b[len]){ len += 1; b[len] = a[i]; } else { pos = lower_bound(b+1, b+1+len, a[i]) - b; b[pos] = a[i]; } } return len;}
以下为Accepted代码
#include <cstdio>#include <algorithm>using namespace std;int n, a[100004], b[100004];int dp1();int dp2();int s(int num, int l, int h);int main(){ int T, i; scanf("%d", &T); while(T--){ scanf("%d", &n); for(i = 1; i <= n; i++) scanf("%d", &a[i]); int x1 = dp1(); int x2 = dp2(); if(x1 == n-1 || x1 == n || x2 == n-1 || x2 == n) printf("YES\n"); else printf("NO\n"); } return 0;}int dp1(){ int i, len, pos; b[1] = a[1]; len = 1; for(i = 2; i <= n; i++){ if(a[i] >= b[len]){ len += 1; b[len] = a[i]; } else { pos = upper_bound(b+1, b+len+1, a[i]) - b; //pos = lower_bound(b+1, b+len+1, a[i]) - b; //pos = s(a[i], 1, len); b[pos] = a[i]; } } return len;}int dp2(){ int i, len, pos; b[1] = a[n]; len = 1; for(i = n-1; i >= 1; i--){ if(a[i] >= b[len]){ len += 1; b[len] = a[i]; } else { pos = upper_bound(b+1, b+len+1, a[i]) - b; //pos = lower_bound(b+1, b+len+1, a[i]) - b; //pos = s(a[i], 1, len); b[pos] = a[i]; } } return len;}int s(int num,int l,int h) { int mid; while(l<=h) { mid=(l+h)/2; if(num>=b[mid]) l=mid+1; else h=mid-1; } return l; }
阅读全文
1 0
- https://vjudge.net/contest/161167#problem/F——dp
- https://cn.vjudge.net/contest/158742#problem/G
- 搜索练习9 https://cn.vjudge.net/contest/81658#problem/J Fire! bfs
- 还是畅通工程 http://vjudge.net/contest/view.action?cid=49515#problem/D
- Oil Deposits 油田,深搜 http://vjudge.net/contest/view.action?cid=49515#problem/G
- Calendar 日历 http://vjudge.net/contest/view.action?cid=49515#problem/I
- 例题1.15 网络 https://vjudge.net/problem/UVALive-3902 树上的搜索
- Vjudge攻略——POJ1753
- Vjudge攻略——POJ2965
- 贪心算法—Problem F
- 动态规划—Problem F
- 图算法—Problem F
- CodeForces 581F Contest Page 【树形dp】
- CF 581F Contest Page 树形DP
- Kids and Prizes 来源: <http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85996#problem/B>
- Vjudge Prime Ring Problem (dfs)
- codeforces contest 855 problem C(树形DP)
- Vjudge攻略——ACdream-1099
- TIANKENG’s restaurant(Ⅱ) hdu 4886 思维暴力
- innodb buffer pool管理--flush list
- 网络通信(Server和Client)
- 第8章 异常控制流
- sqlalchemy ORM
- https://vjudge.net/contest/161167#problem/F——dp
- Android 如何自定义CrashHandler与崩溃后自动重启
- 全行业的关键点是探索数据的价值 挖掘商业模式
- 操作系统概念(高等教育出版社,第七版)复习——第一章:导论
- 新闻客户端
- C++之this指针
- [树的点分治] [POJ2114] Boatherds
- SpringMVC实现服务器端推送
- Supervised Learning 002: k-Nearest Neighbor