HDU 2217 Visit
http://acm.hdu.edu.cn/showproblem.php?pid=2217
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.
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
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int N, T;
int l[maxn], r[maxn];
int ll[maxn], rr[maxn];
bool cmp(int a, int b) {
return a > b;
}
int main() {
while(~scanf("%d%d", &N, &T)) {
memset(ll, 0, sizeof(ll));
memset(rr, 0, sizeof(rr));
int num1 = 0, num2 = 0, Zero = 0;
for(int i = 1; i <= N; i ++) {
int x;
scanf("%d", &x);
if(x > 0)
r[num1 ++] = x;
else if(x < 0)
l[num2 ++] = x;
else
Zero ++;
}
sort(r, r + num1);
sort(l, l + num2, cmp);
for(int i = 0; i < num1; i ++) {
int k = i + 1;
for(int j = 0; j < num2; j ++) {
if(r[i] * 2 + abs(l[j]) <= T)
rr[i] = ++ k ;
else break;
}
}
for(int i = 0; i < num2; i ++) {
int k = i + 1;
for(int j = 0; j < num1; j ++) {
if(abs(l[i]) * 2 + r[j] <= T)
ll[i] = ++ k ;
else
break;
}
}
sort(ll, ll + num2);
sort(rr, rr + num1);
int ans = max(ll[num2 - 1], rr[num1 - 1]);
int cnt = 0;
for(int i = 0; i < num1; i ++)
if(r[i] <= T)
cnt = i;
for(int i = cnt; i < num2; i ++)
if(abs(l[i]) <= T)
cnt = i;
ans = max(ans, cnt);
printf("%d\n", ans + Zero);
}
return 0;
}

浙公网安备 33010602011771号