HDU 4000 Fruit Ninja (树状数组+组合问题)
2015-08-04 16:36
459 查看
找出i<j<k且a[i]<a[k]<a[j] 的个数。
先求出x>y(z)的个数,再求出x>y>z的个数。(small[i]记录当前 1—i-1元素间比a[i]小的个数,high[i]记录当前i+1—n元素比a[i]大的个数),那么x>y>z的个数就为 small[i]*high[i]....
small可以用树状数组求出来!!!!
题意:给你一个1到n的排列,让求满足posx < posy < posz && x < z < y 的组数有多少。
思路:树状数组的题目。首先我们可以得到所有满足题目条件的组数为x < y ? z 减去 x < y < z的数目。其中x < y ? z 是x < y ,但是y 与 z的关系不知道,x 与z 的关系也不知道。假设第i个数后面有n个数比它大,则x < y ? z 的组数为C(n,2),x < y < z的组数可以 用树状数组算出来。
Problem Description
Recently, dobby is addicted in the Fruit Ninja. As you know, dobby is a free elf, so unlike other elves, he could do whatever he wants.But the hands of the elves are somehow strange, so when he cuts the fruit, he can only make specific move of his hands. Moreover,
he can only start his hand in point A, and then move to point B,then move to point C,and he must make sure that point A is the lowest, point B is the highest, and point C is in the middle. Another elf, Kreacher, is not interested in cutting fruits, but he
is very interested in numbers.Now, he wonders, give you a permutation of 1 to N, how many triples that makes such a relationship can you find ? That is , how many (x,y,z) can you find such that x < z < y ?
Input
The first line contains a positive integer T(T <= 10), indicates the number of test cases.For each test case, the first line of input is a positive integer N(N <= 100,000), and the second line is a permutation of 1 to N.
Output
For each test case, ouput the number of triples as the sample below, you just need to output the result mod 100000007.
Sample Input
Sample Output
先求出x>y(z)的个数,再求出x>y>z的个数。(small[i]记录当前 1—i-1元素间比a[i]小的个数,high[i]记录当前i+1—n元素比a[i]大的个数),那么x>y>z的个数就为 small[i]*high[i]....
small可以用树状数组求出来!!!!
题意:给你一个1到n的排列,让求满足posx < posy < posz && x < z < y 的组数有多少。
思路:树状数组的题目。首先我们可以得到所有满足题目条件的组数为x < y ? z 减去 x < y < z的数目。其中x < y ? z 是x < y ,但是y 与 z的关系不知道,x 与z 的关系也不知道。假设第i个数后面有n个数比它大,则x < y ? z 的组数为C(n,2),x < y < z的组数可以 用树状数组算出来。
Problem Description
Recently, dobby is addicted in the Fruit Ninja. As you know, dobby is a free elf, so unlike other elves, he could do whatever he wants.But the hands of the elves are somehow strange, so when he cuts the fruit, he can only make specific move of his hands. Moreover,
he can only start his hand in point A, and then move to point B,then move to point C,and he must make sure that point A is the lowest, point B is the highest, and point C is in the middle. Another elf, Kreacher, is not interested in cutting fruits, but he
is very interested in numbers.Now, he wonders, give you a permutation of 1 to N, how many triples that makes such a relationship can you find ? That is , how many (x,y,z) can you find such that x < z < y ?
Input
The first line contains a positive integer T(T <= 10), indicates the number of test cases.For each test case, the first line of input is a positive integer N(N <= 100,000), and the second line is a permutation of 1 to N.
Output
For each test case, ouput the number of triples as the sample below, you just need to output the result mod 100000007.
Sample Input
2 6 1 3 2 6 5 4 5 3 5 2 4 1
Sample Output
Case #1: 10 Case #2: 1
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n; const int maxn=111111; int a[maxn],b[maxn]; __int64 small[maxn],high[maxn]; void update(int x,int num) { while(x<=n){ a[x]+=num; x+=x&(-x); } } __int64 getsum(int x) { __int64 s=0; while(x>0){ s+=a[x]; x-=x&(-x); } return s; } int main() { int i,j,t; __int64 ans; while(scanf("%d",&t)!=EOF){ for(int _=1;_<=t;_++){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); scanf("%d",&n); ans=0; for(i=1;i<=n;i++){ scanf("%d",&b[i]); small[i]=getsum(b[i]); high[i]=n-b[i]-(i-1-small[i]); update(b[i],1); } for(i=1;i<=n;i++){ ans+=high[i]*(high[i]-1)/2-small[i]*high[i]; } printf("Case #%d: %d\n",_,ans%100000007); } } return 0; }
相关文章推荐
- 简单实现UGUI拖拽功能
- Lucene各种Query
- POJ2299 Ultra-QuickSort
- UVa1605 - Building for UN(构造法)
- make menuconfig提示'make menuconfig' requires the ncurses libraries.
- GUI编程,单选按钮、复选框、消息框、消息响应函数的使用
- IOS学习之UITableView(三):进阶篇索引,标记和自定义的table
- IOS中UIActionSheet使用详解
- android 注解 guice.jar ,roboguice.jar 使用
- UIDevice 、NSBundle、NSLocale
- easyui combogrid 多选加载,保存,显示代码
- 关于uitableviewcell的accessoryType属性
- HDU1242:Rescue bfs和dfs
- EasyUI——常见用法总结
- (一〇六)iPad开发之UIPopoverController的使用
- (一〇六)iPad开发之UIPopoverController的使用
- iOS 并发编程之 Operation Queues
- UINavigationBar-使用总结
- StringBuffer跟StringBuilder的区别
- UIModalPresentationStyle和UIModalTransitionStyle