HDU2217Visit
来源:互联网 发布:梦想成真软件系统 编辑:程序博客网 时间:2024/06/05 21:56
Visit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 644 Accepted Submission(s): 250
Problem Description
Wangye is interested in traveling. One day, he want to make a visit to some
different places in a line. There are N(1 <= N <= 2000) places located at points x1, x2, …, xN (-100,000 ≤ xi ≤ 100,000). Wangye starts at HDU (x = 0), and he travels 1 distance unit in 1 minute . He want to know how many places he could visit at most, if he has T (1 <= T <= 200000 ) minutes.
Input
The input contains several test cases .Each test case starts with two number N and T which indicate the number of places and the time respectively. Then N lines follows, each line has a number xi indicate the position of i-th place.
Output
For each test case, you should print the number of places,Wangye could visit at most, in one line.
Sample Input
5 16
-3
-7
1
10
8
Sample Output
4
Hint
In the sample, Wangye could visit (-3) first, then goes back to (0), which costs him 6 minutes.
Then he visits (1), (8), (10). So he could visit 4 places at most in 16 minutes. :
Author
zjt
题意:给你n个点和t时间和每个点的坐标,问在t时间内最多能经过多少个点。
题解:
将点分成正负两个集合。枚举每一个点作为端点的情况,记录最大值即可。这里用了一个前缀和,记录距离<=i有多少个点。
注意:多点在一个地方的情况和初始点(0)是否有点的情况
代码:
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <math.h>#include <algorithm>#include <string.h>using namespace std;const int N=1e6+10;int l[N],r[N];int a[N],b[N];int n,t;int main(){ while(~scanf("%d%d",&n,&t)) { memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int tmp,tmpp,x; tmp=tmpp=1; int sum=0; for(int i=1;i<=n;i++) { scanf("%d",&x); if(x>0) { l[tmp++]=x; a[x]++; } else if(x<0) { r[tmpp++]=-x; b[-x]++; } else { sum++; } } sort(l+1,l+tmp); sort(r+1,r+tmpp); for(int i=1;i<N;i++) { a[i]+=a[i-1]; b[i]+=b[i-1]; } int ans=0; for(int i=0;i<tmpp;i++) { if(t-2*r[i]>=0) ans=max(ans,a[t-2*r[i]]+b[r[i]]); } for(int i=0;i<tmp;i++) { if(t-2*l[i]>=0) ans=max(ans,b[t-2*l[i]]+a[l[i]]); } ans+=sum; printf("%d\n",ans); }}
#include <iostream>#include<stdio.h>#include<algorithm>using namespace std;int n,T,a[2005];int main(){ int s,q,p,ma,x,y; while(scanf("%d %d",&n,&T)!=EOF) { a[0]=0; for(int i=1; i<=n; i++) scanf("%d",&a[i]); sort(a,a+n+1); s=lower_bound(a,a+n+1,0)-a; ma=0; for(q=s;q>=0;q--) { for(p=s;p<=n;p++) { x=-a[q],y=a[p]; if(2*x+y<=T&&p-q>ma) ma=p-q; if(2*y+x<=T&&p-q>ma) ma=p-q; } } cout<<ma<<endl; } return 0;}