HDU 4000 Fruit Ninja

2024-01-17 00:32
文章标签 hdu 4000 fruit ninja

本文主要是介绍HDU 4000 Fruit Ninja,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参看资料:

https://blog.csdn.net/keepcoral/article/details/80550168


题目:

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

题目大意:

        给定 N 个数的数组A,问能够找到多少(x,y,z),使得 x< y< z,且 A[ x ]<A[ z ]<A[ y ]  。

解题思路:

        要以整体【整个数组】的思想来看这道题,就会明白这个人到底在做什么。

        当我们输入第 i 个数字 a 时,用的树状数组存储 sum(a-1) 【sum(a-1) 表示:在a这个位置的前 i-1 个数中,有sum(a-1)个数比 a 小】。

        那么,就可以求出在 a 后面的 (i+1,n) 的序列中,有 R=(n-a)-(i-1-sum(a-1)) 个数比a要大【(n-a)表示有这么多个数比a大,(i-1-sum(a-1))表示前i-1个数里面有sum(a-1)个数比a大】。

       然后,让a当作三个数里面的最小值,以此取出三个数的组合,就有 C(R,2) 即 R*(R-1)/2 ,因为后面的序列都是固定的,所以并没有排序的概念。【例:原本序列1 5 4 ,那么我们没有必要去思考1 4 5的情况,因为它已经固定好了(一开始死都想不通....)】。

       所以后面的序列选的时候直接从R个选2个就可以了,然后C(R,2)表示所有的a与两个大于 a 的数的组合【也就是小中大,小大中都包含在里面了】。那么我们要求的是小大中,所以对于每一个a来说,我们都需要减去 “小中大” 的情况,那么小中大=sum(a-1)*R,所以最后答案就是对于(1,n)所有数,求和SUM(C(R,2)-sum(a-1)*R)

 

实现代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
ll c[100009];
ll lowbit(ll x){return x&(-x);
}
ll getsum(ll x){ll sum=0;while(x){sum+=c[x];x-=lowbit(x);}return sum;
}
void update(ll x,ll val){while(x<=n){c[x]+=val;x+=lowbit(x);}
}
int main(){int i,T;scanf("%d",&T);int Count=1;while(T--){ll ans=0;scanf("%d",&n);memset(c,0,sizeof(c));for(i=1; i<=n; i++){ll a,L,R;scanf("%lld",&a);update(a,1);L=getsum(a-1);  //Left表示有多少个数比a小,R=(n-a)-(i-1-L);//前面n-a的含义是有多少个数比a大//(i-1-Left)表示i-1个数里面有这么多个数比a大ans+=R*(R-1)/2-L*R;}printf("Case #%d: %lld\n",Count++,ans%100000007);}
}

 

 

这篇关于HDU 4000 Fruit Ninja的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/614402

相关文章

青龙面板之Ninja无法安装无法拉库问题解决

因为之前的Ninja库已经不能用了,甚至新找到的库也不能用了,好尴尬,这里使用线下版本进行安装。 ninja安装新方法,其是方法还是原来的,只不过Ninja的库原作者删了,没法直接git了,但是我找到了源码包,我们可以直接通过宝塔面板拖进去。 源码包地址: https://download.csdn.net/download/u012134073/24813485 备用地址: 链接: h

开启青龙 Ninja 扫码功能失效后修改成手动填写CK功能【修正Ninja拉库地址】

国内:进入容器docker exec -it qinglong bash #获取ninjagit clone -b main https://ghproxy.com/https://github.com/wjx0428/ninja.git /ql/ninja#安装cd /ql/ninja/backend && pnpm install cp .env.example .env

上海邀请赛 A题目 HDU 5236(dp)

先求出没有ctrl+s的时候构造长度为i的期望f[i] 。然后枚举保存的次数,求出最小即可。 #include<cstdio>#include<cstdio>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<iostream>#include<map>

hdu 2586 树上点对最近距离 (lca)

,只要知道dis[i][j]=dis[i][root]+dis[j][root]-2*dis[Lca(i,j)][root].   其中root为树的根节点,LCA(i,j)为i,j的最近公共祖先。 所以我们先把所有的询问储存下来,然后离线直接查询。复杂度是o(n+q)的。 VIE #include<cstdio>#include<algorithm>#include<i

HDU_1013

这个题目尤其需要注意的是开始输入的时候的数的大小,开始输入时有可能非常的大超过了长整型的范围,所以不能开始用整形来存放输入的,,就是开始的时候没有注意到这个问题所以开始的时候一直没有AC,后来就是用一个数组接收输入,然后在经过第一步转化之后就可以用一个整数来装了 #include <iostream>#include <stdlib.h>#include <string>usin

最长回文子串(百度笔试题和hdu 3068)

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/17123559 求一个字符串的最长回文子串。注意子串是连续的,子序列是不连续的。对于最长回文子序列,要用动态规划解,具体请看: http://blog.csdn.net/xiaofei_it/article/details/1

杭电ACM hdu 2110 Crisis of HDU 解题报告(母函数)

Problem Description 话说上回讲到HDU大战东洋小苟,结果自然是中方大胜,这一战也使得海东集团在全球同行业中的地位更加巩固。随着集团的发展,很多创业时期的元老逐步功成身退,先是8600移民海外,然后是linle夫妇退隐山林,逐渐的,最初众多的元老只剩下XHD夫妇和Wiskey三人了。 到了2020年,因为扩张过度加上老鼠数量逐年减少,公司的发展遇到了前所未有的危机,此时集团已经

杭电ACM hdu 2082 找单词 解题报告(母函数)

Problem Description 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26。那么,对于给定的字母,可以找到多少价值<=50的单词呢?单词的价值就是组成一个单词的所有字母的价值之和,比如,单词ACM的价值是1+3+14=18,单词HDU的价值是8+4+21=33。(组成的单词与排列顺序无关,比如

杭电ACM hdu 2079 选课时间 解题报告(母函数)

Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合。你来帮帮他吧。(xhd认为一样学分的课没区别)   Input 输入数据的第一行是一个数据T,表示有T组数据。 每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8)。 接着有k行,每行有两个整数a(1 <= a <= 8),b

[HDU 4855] Goddess (极角排序+三分)

HDU - 4855 借这道题学了下极角排序和三分求凸函数最大值 按所有左右切线,圆心的弧度值排序,从而分割出角度上的若干个区间 射线的角度在这些区间里变动时,每个割线的长度都是凸函数,叠加起来也是凸函数 所以可以对每个区间利用三分法求得最大值,再对这些最大值取 max 即为答案 1) 利用三分法可以求得单峰函数的最大值 2) 利用 atan2(y,x)可以比较方便地求出向量与 x轴正