Snuke Festival(二分法)
題目描述
The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.
He has N parts for each of the three categories. The size of the i-th upper part is Ai, the size of the i-th middle part is Bi, and the size of the i-th lower part is Ci.
To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.
How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.
Constraints
1≤N≤105
1≤Ai≤109(1≤i≤N)
1≤Bi≤109(1≤i≤N)
1≤Ci≤109(1≤i≤N)
All input values are integers.
?
輸入
Input is given from Standard Input in the following format:
N
A1 … AN
B1 … BN
C1 … CN
?
輸出
Print the number of different altars that Ringo can build.
?
樣例輸入
2 1 5 2 4 3 6?
樣例輸出
3?
提示
The following three altars can be built:
Upper: 1-st part, Middle: 1-st part, Lower: 1-st part
Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part
Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part
?
?
binary函數寫的太麻煩了,不如分開寫,不過總算是通過了。
在數據量很大的時候,使用sort函數排序之后在用二分法是很快的。
代碼:
#include <iostream> ? ??
#include <algorithm>?? ??? ??
using namespace std;
typedef long long ll;
const int inf=1e5+10;
int A[inf],B[inf],C[inf];
ll binary(int *arr,int low,int high,int temp,int flag) ?
{
?? ?while(low<=high) ?
?? ?{
?? ??? ?int middle=(low+high)/2;
?? ??? ?if(arr[middle]==temp) ? ? ?
?? ??? ?{
?? ??? ??? ?if(flag==1)
?? ??? ??? ?{
?? ??? ??? ??? ?high=middle;
?? ??? ??? ??? ?while(low<=high)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?middle=(low+high)>>1;
?? ??? ??? ??? ??? ?if(arr[middle]>=temp) high=middle-1;
?? ??? ??? ??? ??? ?else low=middle+1;
?? ??? ??? ??? ?}?
?? ??? ??? ??? ?return high+1;?? ??
?? ??? ??? ?}?? ?
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?low=middle;
?? ??? ??? ??? ?while(low<=high)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?middle=(low+high)>>1;
?? ??? ??? ??? ??? ?if(arr[middle]<=temp)low=middle+1; ?
?? ??? ??? ??? ??? ?else high=middle-1;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return low-1; ? ? ? ? ? ? ? ? ? ? ? ? ??
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(arr[middle]<temp)
?? ??? ??? ?low=middle+1;
?? ??? ?if(arr[middle]>temp)
?? ??? ??? ?high=middle-1;?? ?
?? ?}
?? ?return high;
}?
int main()
{
?? ?int n;
?? ?scanf("%d",&n);
?? ?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]);
?? ?sort(A+1,A+n+1);
?? ?sort(B+1,B+n+1);?? ?
?? ?sort(C+1,C+n+1);
?? ?long long sum=0;?
?? ?for(int i=1;i<=n;i++)
?? ?{?? ??? ??? ??? ??? ??? ??? ??? ??? ?
?? ??? ?ll ans1=binary(A,1,n,B[i],1); ? ? ?
?? ??? ?if(A[ans1]==B[i])
?? ??? ??? ?ans1--;?
?? ??? ?ll ans2=binary(C,1,n,B[i],3); ? ? ? ?
?? ??? ?ans2=n-ans2;
?? ??? ?sum=sum+ans1*ans2;?? ??? ?
?? ?}
?? ?printf("%lld\n",sum);?? ??? ??? ? ?? ?
?? ?return 0;
}
總結
以上是生活随笔為你收集整理的Snuke Festival(二分法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二分法(递归非递归)
- 下一篇: 二维前缀和。