记录编号 119822 评测结果 AAAAAAAAAA
题目名称 三元数对 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.045 s
提交时间 2014-09-14 15:16:35 内存使用 0.99 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const LL INF=0x7fffffff;
//==============struct declaration==============
struct node
{
  int no,val;
  bool operator <(const node &rhs) const
  {
    return val<rhs.val;
  }
}temp[30010];
//==============var declaration=================
const int MAXN=30010;
int n;
int num[MAXN],sum[MAXN];
LL L[MAXN],R[MAXN],res=0;
//==============function declaration============
int lowbit(int x);
void add(int x);
int query(int x);
//==============main code=======================
int main()
{  
  string FileName="three";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
  clock_t Start_Time=clock();
#endif    
  scanf("%d",&n);
  For(i,1,n)
  {
    temp[i].no=i;
    scanf("%d",&temp[i].val);
  }
  sort(temp+1,temp+n+1);
  for(int i=1,pos=1;i<=n;i++,pos++)
  {
    num[temp[i].no]=pos;
    if (temp[i].val==temp[i+1].val)
      pos--;
  }
  /*For(i,1,n)
    printf("%d ",num[i]);*/
  memset(sum,0,sizeof(sum));
  For(i,1,n)
  {
    L[i]=query(num[i]-1);
    add(num[i]);
  }
  memset(sum,0,sizeof(sum));
  for(int i=n;i>=1;i--)
  {
    int t=query(num[i]);//在i的右边有t个数小于等于它,一共有n-i个数,所以有n-i-t个数大于它 
    add(num[i]);
    R[i]=n-i-t;
  }
  For(i,1,n)
    res+=L[i]*R[i];
  pritnf("%lld\n",res);
#ifdef DEBUG  
  clock_t End_Time=clock();
  printf("\n\nTime Used: %.4lf Ms\n",double(End_Time-Start_Time)/CLOCKS_PER_SEC);
#endif    
  return 0;
}
//================fuction code====================
int lowbit(int x)
{
  return x&-x;
}
void add(int x)
{
  while (x<=n)
  {
    sum[x]++;
    x+=lowbit(x);
  }
}
int query(int x)
{
  int ans=0;
  while (x>0)
  {
    ans+=sum[x];
    x-=lowbit(x);
  }
  return ans;
}