Enemy is weak
来源:互联网 发布:怎样注册农村淘宝网店 编辑:程序博客网 时间:2024/05/25 13:33
The Romans have attacked again. This time they are much more than the Persians but Shapur 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 tripletsi, 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 trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
33 2 1
1
32 3 1
0
410 8 3 1
4
41 5 4 3
1
题解:求出左端比该位置大的元素和右端比该位置小的元素。
#include<iostream>#include<stdio.h>#include<algorithm>#include<vector>typedef long long ll;const int Maxn=1e6+10;using namespace std;int n,sums[Maxn],suml[Maxn];ll l[Maxn],s[Maxn];struct nod{ int val,index;}a[Maxn];bool cmp1(nod a,nod b){ return a.val>b.val;}bool cmp2(nod a,nod b){ return a.val<b.val;}int lowbit(int x){ return x&(-x);}void updata(int x){ while(x<=n){ suml[x]++; x+=lowbit(x); }}void downdata(int x){ while(x>0){ sums[x]++; x-=lowbit(x); }}ll getSuml(int x){ ll ret=0; while(x>0){ ret+=suml[x]; x-=lowbit(x); } return ret;}ll getSums(int x){ ll ret=0; while(x<=n){ ret+=sums[x]; x+=lowbit(x); } return ret;}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++) suml[i]=0,sums[i]=0; for(int i=1;i<=n;i++) scanf("%d",&a[i].val),a[i].index=i; sort(a+1,a+1+n,cmp1); ll ans=0; for(int i=1;i<=n;i++) l[a[i].index]=getSuml(a[i].index),updata(a[i].index); sort(a+1,a+1+n,cmp2); for(int i=1;i<=n;i++) s[a[i].index]=getSums(a[i].index),downdata(a[i].index); for(int i=1;i<=n;i++) ans+=l[a[i].index]*s[a[i].index]; printf("%I64d\n",ans); }}
这种方法简单一些。。。
#include<iostream>#include<stdio.h>#include<algorithm>#include<vector>typedef long long ll;const int Maxn=1e6+10;using namespace std;int n;struct nod{ int val,index;}a[Maxn];bool cmp(nod a,nod b){ return a.val>b.val;}int lowbit(int x){ return x&(-x);}struct st{ ll sum[Maxn]; void updata(int x,ll val){ while(x<=n){ sum[x]+=val; x+=lowbit(x); } } ll getSum(int x){ ll ret=0; while(x>0){ ret+=sum[x]; x-=lowbit(x); } return ret; }};st sum1,sum2;int main(){ //freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++) sum1.sum[i]=0,sum2.sum[i]=0; for(int i=1;i<=n;i++) scanf("%d",&a[i].val),a[i].index=i; sort(a+1,a+1+n,cmp); ll ans=0; for(int i=1;i<=n;i++){ ll cnt1=sum1.getSum(a[i].index);//比该位置元素大个数 ll cnt2=sum2.getSum(a[i].index);//比该位置元素大对数 ans+=cnt2; sum1.updata(a[i].index,1); sum2.updata(a[i].index,cnt1); } printf("%I64d\n",ans); }}
- 【Codeforces61E】Enemy is weak
- Enemy is weak
- Enemy is weak 树状数组
- 61E Enemy is weak
- CF E. Enemy is weak 线段树
- CodeForces - 61E Enemy is weak
- CF - 61E - Enemy is weak(树状数组)
- Codeforces Beta Round #57 (Div. 2) E. Enemy is weak
- Codeforces Beta Round #57 (Div. 2) Enemy is weak
- CodeForces 61E Enemy is weak 【树状数组】
- cf 61E. Enemy is weak 树状数组求逆序数
- cf 61E. Enemy is weak 树状数组求逆序数(WA)
- Codeforces Beta Round #57 (Div. 2)E---Enemy is weak(树状数组+离散化)
- Complexity is The Enemy
- Codeforces 61E Enemy is weak 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组
- 三元逆序对 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组Codeforces 61E Enemy is weak
- enemy
- Scope Is the Enemy of Success
- Hibernate之查询
- 2017中北大学程序设计大赛决赛题解
- FFmpeg常用命令记录(1):录取屏幕、剪切视频、将视频转化为GIF
- jquery入口函数
- PageRank算法
- Enemy is weak
- Swift 文件 只有 import Foundation 而需要UIViewController
- MByteToWChar WCharToMByte
- TensorFlow CIFAR-10数据集
- CSS设计美丽之横排页面(小作品)
- nginx 点播mp4方法
- RSA私钥和公钥文件格式 (pkcs#7, pkcs#8, pkcs#12, pem)
- HSSFSheet导出excel
- 二维数组