第一种做法:前缀和 O( n )
第二种做法:排序+二分 O(nlgn)
前缀和
空间换时间
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], b[N], c[N];
int cnta[N], cntc[N];
//cnta数组含义: cnta[i] a中i出现的次数
int main()
{
int n;cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i],a[i]++;
for (int i = 1;i <= n;i++)
cin >> b[i],b[i]++;
for (int i = 1;i <= n;i++)
cin >> c[i],c[i]++;
for (int i = 1;i <= n;i++)
cnta[a[i]]++, cntc[c[i]]++;
for (int i = 1;i <= N;i++)
{//这里ABC数组里最大值时10的五次方
cnta[i] += cnta[i - 1];
cntc[i] += cntc[i - 1];
}
long long sum = 0;
for (int i = 1;i <= n;i++)
sum += (long long)cnta[b[i]-1] * (cntc[N-1]-cntc[b[i]]);
//sum += (long long)cnta[b[i]-1] * (n-cntc[b[i]]);//也可以这样写
cout << sum;
system("pause");
}
二分+排序
这里的二分还得处理特殊情况 ( if (a[l] >= x) l = 0; 根据结果的正确性返回一个值 )
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], b[N], c[N];
int n;
int find(int x)
{
int l = 1;int r = n;
if (a[l] >= x) l = 0;
while (l < r)
{
int mid = l + r + 1 >> 1;
if (a[mid] < x) l = mid;
else r = mid - 1;
}
return l;//返回下标
}
int find2(int x)
{
int l = 1;int r = n;
if (c[r] <= x) l = n+1;
while (l < r)
{
int mid = l + r >> 1;
if (c[mid] > x) r = mid;
else l = mid + 1;
}
return l;//返回下标
}
int main()
{
cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i];
for (int i = 1;i <= n;i++)
cin >> b[i];
for (int i = 1;i <= n;i++)
cin >> c[i];
sort(a+1, a + n+1);
sort(c+1, c + n+1);
long long sum = 0;
for (int i = 1;i <= n;i++)
{
sum = sum + (long long)find(b[i]) * (n+1-find2(b[i]));
}
cout << sum;
system("pause");
}
二分统一形式👇
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], b[N], c[N];
int n;
int find(int x,int a[])
{
int l = 1;int r = n;
if (a[l] >= x) l = 0;
while (l < r)
{
int mid = l + r + 1 >> 1;
if (a[mid] < x) l = mid;
else r = mid - 1;
}
return l;//返回下标
}
int main()
{
cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i];
for (int i = 1;i <= n;i++)
cin >> b[i];
for (int i = 1;i <= n;i++)
cin >> c[i];
sort(a + 1, a + n + 1);
sort(c + 1, c + n + 1);
long long sum = 0;
for (int i = 1;i <= n;i++)
{
sum = sum + (long long)find(b[i],a) * (n - find(b[i]+1,c));
}
cout << sum;
system("pause");
}
直接调用upper_bound 和lower_bound
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int num[3][N];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < 3; ++i)
for (int j = 1; j <= n; ++j)
scanf("%d", &num[i][j]);
for (int i = 0; i < 3; ++i)
sort(num[i] + 1, num[i] + n + 1);
LL ans = 0;
for (int i = 1; i <= n; ++i) {
int key = num[1][i];
//A中二分查找第一个小于key的数的下标
int pos1 = lower_bound(num[0] + 1, num[0] + n + 1, key) - num[0] - 1;
//C中二分查找第一个大于key的数的下标
int pos2 = upper_bound(num[2] + 1, num[2] + n + 1, key) - num[2];
if (pos1 >= 1 && pos2 <= n) {
ans += (LL)pos1 * (n - pos2 + 1);
}
}
cout << ans << endl;
system("pause");
return 0;
}
二分模板
模板还是要记住的,毕竟上面的那个函数只能处理大于等于 不等价于这里的check
如何记忆理解:对于如何判断用r=mid or l=mid
我们不妨先从左到右遍历一下区间 并代入l=mid 与check 的条件 判断是否会筛去我们想要的结果值。若筛去了,则从右到左遍历区间 并代入r=mid 与check的条件 判断是否会筛去(若前者筛去了这里就不会筛去了)
字符串转数字
外加一个“2019”字符串转数字 2019的方法 (stoi 也可以
int x=0;
for( i = 0 ; i < s.size() ; i++ )
x=x*10 + str[i]-' 0 ';