最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

CodeforcesRound#261(Div.2)D.PashmakandParmida&#3

来源:动视网 责编:小采 时间:2020-11-09 08:09:35
文档

CodeforcesRound#261(Div.2)D.PashmakandParmida&#3

CodeforcesRound#261(Div.2)D.PashmakandParmida:Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence a that con
推荐度:
导读CodeforcesRound#261(Div.2)D.PashmakandParmida:Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence a that con


Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence a that con

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

There is a sequence a that consists of n integers a1,?a2,?...,?an. Let's denotef(l,?r,?x) the number of indicesk such that: l?≤?k?≤?r andak?=?x. His task is to calculate the number of pairs of indiciesi,?j (1?≤?i?j?≤?n) such thatf(1,?i,?ai)?>?f(j,?n,?aj).

Help Pashmak with the test.

Input

The first line of the input contains an integer n(1?≤?n?≤?106). The second line containsn space-separated integers a1,?a2,?...,?an(1?≤?ai?≤?109).

Output

Print a single integer — the answer to the problem.

Sample test(s)

Input

7
1 2 1 1 2 2 1

Output

8

Input

3
1 1 1

Output

1

Input

5
1 2 3 4 5

Output

0


由于原数过大,可使用map 映射,就不需要离散化了~两个数组,一个记录前面与本数相等的数的个数,一个记录后面与本数相等的数的个数~

题目便转化成求逆序数对,树状数组。答案long long

CODE:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
const int inf=0xfffffff;
typedef long long ll;
using namespace std;

const int Max=1000006;
int num[Max];
int f1[Max],f2[Max];
int c[Max];
mapm1,m2;
int n;

int lowbit(int x)
{
 return x&-x;
}
void add(int x,int d)
{
 while(x<=n){
 c[x]+=d;
 x+=lowbit(x);
 }
}
int sum(int x)
{
 int ans=0;
 while(x>0){
 ans+=c[x];
 x-=lowbit(x);
 }
 return ans;
}
int main()
{
 //freopen("in","r",stdin);
 while(~scanf("%d",&n)){
 m1.clear();
 m2.clear();
 memset(f1,0,sizeof(f1));
 memset(f2,0,sizeof(f2));
 memset(c,0,sizeof(c));
 for(int i=1;i<=n;i++){
 scanf("%d",&num[i]);
 }
 for(int i=1;i<=n;i++){
 f1[i]=(++m1[num[i]]);
 }
 for(int i=n;i>=1;i--){
 f2[i]=(++m2[num[i]]);
 }
 ll ans=0;
 for(int i=n-1;i>=1;i--){
 add(f2[i+1],1);
 ans+=(ll)sum(f1[i]-1);
 }
 printf("%I64d\n",ans);
 }
 return 0;
}

文档

CodeforcesRound#261(Div.2)D.PashmakandParmida&#3

CodeforcesRound#261(Div.2)D.PashmakandParmida:Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence a that con
推荐度:
标签: and code round
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top