two point
来源:互联网 发布:不可而知 编辑:程序博客网 时间:2024/06/05 05:57
名字是zhw起的。
有这样一个问题:
解法:
利用两个指针,其中一个从前往后扫a数组,另一个从后往前扫b数组,先固定其中一个,另一个来扫
用可能成为最大值的数来更新答案。
具体看代码吧:
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;int n,p,ans;int a[100009],b[100009];int main(){ scanf("%d%d",&n,&p); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); int t1=1,t2=n; while(t1<=n&&t2>=1) { while(a[t1]+b[t2]>p&&t2>=1) t2--; ans=max(ans,a[t1]+b[t2]); t1++; } printf("%d",ans); return 0;}
解法:对a,b数组用two point,同时求c,d的最大前缀和(即前面的最大的那个数),不断更新答案。
代码:
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#define M 100009using namespace std;int n,p,ans;int a[M],b[M],c[M],d[M];int main(){ scanf("%d%d",&n,&p); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); for(int i=1;i<=n;i++) scanf("%d",&c[i]); for(int i=1;i<=n;i++) scanf("%d",&d[i]); for(int i=1;i<=n;i++) c[i]=max(c[i],c[i-1]),d[i]=max(d[i],d[i-1]);//前缀最大值 int t1=1,t2=n; while(t1<=n&&t2>=1) { while(a[t1]+b[t2]>p&&t2>=1) t2--; ans=max(ans,c[t1]+d[t2]); t1++; } printf("%d",ans); return 0; }/*5 91 2 3 4 51 3 5 7 95 6 7 9 81 2 50 100 10*/
阅读全文
0 0
- two point
- Two point
- 【Hackerrank】Find the merge point of two joined linked lists
- hdu 5288 OO’s Sequence(two point + rmq)
- LeetCode Two Point & Array Problem 两点问题汇总
- Least-Squares Fitting of Two 3-D Point Sets
- 6-14 Add Two Polynomials(20 point(s))
- Point
- Point
- point
- Point
- point
- Point
- Find the first intersection of two lists (if the two list are merged at some point)
- CodeForces 293E Close Vertices(点分治+Two Point法+树状数组)
- 6-18 Two Stacks In One Array(20 point(s))
- Appliation Loader上传App 莫名其妙的错误 The CFBundleExecutable of two bundles may not point to the same file.
- Point to Point Communication
- ElasticSearch学习笔记(二)IK分词器和拼音分词器的安装
- JSP需要掌握的内容
- 发布自己的npm包及报错解决办法
- 程序员不能只会敲代码还要会投资理财
- Android_ADB
- two point
- PHP的静态及类中声明的常量
- HDU1028
- spirngMVC工作原理学习
- redis搭建
- 欢迎使用CSDN-markdown编辑器
- printf()函数什么时候刷新缓存
- Mac OS上安装使用MongoDB
- Java复习-面向对象的特征