日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Snuke Festival(二分法)

發布時間:2024/10/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(二分法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。