UESTC Training for Data Structures——A
来源:互联网 发布:软件开发的难度 编辑:程序博客网 时间:2024/05/16 17:33
Problem A
Problem Description
The Romans have attacked again,This time they are much more than the Persians but Shapue is ready to defeat them.He says:"A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them,So he gives the army a weakness number.In Shapur's opinion the weakness of an army is equal to the number of triplets i,j,k such that i<j<k and ai>aj>ak,where ax is the power of man standing at position x.The Roman army has one special trail -- powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line is an integer T(T<=10)--The number of test cases.
For each of the test case,the first line contains a single number n(3<=n<=10^6)--the number of men in Roman army.The next line contains N different possitive integers ai(1<=ai<=10^6)--power of men in the Romen army.
For each of the test case,the first line contains a single number n(3<=n<=10^6)--the number of men in Roman army.The next line contains N different possitive integers ai(1<=ai<=10^6)--power of men in the Romen army.
Output
For each of the T cases,output a single number,the weakness of the Roman army.
Sample Input
333 2 132 3 1410 8 3 1
Sample Output
104
/*将原来的数据按照从小到大一次hash成1--n,然后每次计算[1,i]中比a[i]小的元素的个数x每次ans增加的值就是 (i-1-x)*(a[i]-1-x),最后输出ans的值即可*/#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<ctime>#define N 1000006using namespace std;struct data{ int num,id;} b[N];int a[N];int sum[N];void qsort(int st,int en) //对输入的数据进行排序{ srand((unsigned)time(NULL)); //产生一个st到en的随机数 ll int ll=rand()%(en-st)+st; int i=st,j=en; b[0]=b[ll]; b[ll]=b[st]; b[st]=b[0]; while(i<j) { while(i<j && b[0].num<=b[j].num) j--; if(i<j) { b[i]=b[j]; i++; } while(i<j && b[0].num>=b[i].num) i++; if(i<j) { b[j]=b[i]; j--; } } b[i]=b[0]; if(st<i-1) qsort(st,i-1); if(i+1<en) qsort(i+1,en);}__int64 get_sum(int *a,int pos) //求和{ __int64 temp=0; while(pos>0) { temp+=a[pos]; pos-=pos&(-pos); } return temp;}void update(int *a,int pos,int k) //更新{ while(pos<N) { a[pos]+=k; pos+=pos&(-pos); }}int main(){ int t,n; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(sum,0,sizeof(sum)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); b[i].num=a[i]; b[i].id=i; } qsort(1,n); for(int i=1;i<=n;i++) a[b[i].id]=i; //将原来的数据按照从小到大hash成1--n __int64 ans=0; for(int i=1;i<=n;i++) { __int64 x=get_sum(sum,a[i]); //找到[1,i]中,比a[i]小的元素的个数 ans+=(i-1-x)*(a[i]-1-x); update(sum,a[i],1); //将a[i]的元素的个数增加1个 } printf("%I64d\n",ans); } return 0;}
- UESTC Training for Data Structures——A
- UESTC Training for Data Structures——B
- UESTC Training for Data Structures——C
- UESTC Training for Data Structures——D
- UESTC Training for Data Structures——E
- UESTC Training for Data Structures——F
- UESTC Training for Data Structures——G
- UESTC Training for Data Structures——H
- UESTC Training for Data Structures——I
- UESTC Training for Data Structures——J
- UESTC Training for Data Structures——K
- UESTC Training for Data Structures——L
- UESTC Training for Data Structures——M
- 2014 UESTC Training for Data Structures—— E - 休生伤杜景死惊开
- 【UESTC】2015 UESTC Training for Data Structures
- 2016 UESTC Training for Data Structures A题 线段树
- UESTC Training for Data Structures(总结)
- 2016 UESTC Training for Data Structures
- 套接字编程中,recv error:bad file descriptor
- UESTC Training for Data Structures(总结)
- hdu 1964 Pipes
- java正则表达式
- bootmem allocator
- UESTC Training for Data Structures——A
- 用mysqlslap对MySQL进行压力测试
- Hibernate入门
- C++泛型编程:源起、实现与意义
- POJ3041 二分图最大匹配(网络流算法)
- 顺序点
- poj 1739 Tony's Tour
- C++中extern “C”含义深层探索
- C程序模拟停车场管理问题