思路:牛的个数可以达到10的6次方,暴力时间肯定是过不了的,可以先排序,然后再二分找到第一个大于本身加上d距离的,那么前一个就是最后一个
ac:
#include "iostream"
#include "algorithm"
using namespace std;
int n, d;
long long sum = 0;
int a[1000005];
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + 1 + n, less<int>());
for (int i = 1; i <= n; i++) {
int temp = upper_bound(a + 1, a + 1 + n, a [i] + d)-a; //没有则返回a+1+n
sum += temp - i - 1; //temp是第一个大于a+d的所以temp-1就是最后一个满足的
}
cout << sum;
}
ac:
#include "iostream"
using std::cin;
using std::cout;
using std::endl;
int h, w, n,z;
int sum = 0;
char b[505];
char s[505][505];
int temp1,temp2;
int main() {
cin >> h >> w >> n;
for (int i = 0; i < n; i++) {
cin >> b[i];
}
for (int i = 0; i < h; i++)
cin >> *(s + i);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
if (s[i][j] == '.') {
temp1 = i, temp2 = j;
for (z = 0; z < n; z++)
{
if (b[z] == 'L')temp2 -= 1;
if (b[z] == 'R')temp2 += 1;
if (b[z] == 'U')temp1 -= 1;
if (b[z] == 'D')temp1 += 1;
if (s[temp1][temp2] == '#'||temp1<0||temp2<0||temp1>=h||temp2>=w)break;
}
if (z == n)sum++;
}
}
cout << sum;
}