HDU 5419 Victor and Toys ()
来源:互联网 发布:mac需要键盘膜吗 编辑:程序博客网 时间:2024/05/21 09:06
Victor and Toys
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 467 Accepted Submission(s): 157
Problem Description
Victor has n toys, numbered from 1 to n . The beauty of the i -th toy is wi .
Victor has a sense of math and he generatesm intervals, the i -th interval is [li,ri] . He randomly picks 3 numbers i,j,k(1≤i<j<k≤m) , and selects all of the toys whose number are no less than max(li,lj,lk) and no larger than min(ri,rj,rk) . Now he wants to know the expected sum of beauty of the selected toys, can you help him?
Victor has a sense of math and he generates
Input
The first line of the input contains an integer T , denoting the number of test cases.
In every test case, there are two integersn and m in the first line, denoting the number of the toys and intervals.
The second line containsn integers, the i -th integer wi denotes that the beauty of the i -th toy.
Then there arem lines, the i -th line contains two integers li and ri .
1≤T≤10 .
1≤n,m≤50000 .
1≤wi≤5 .
1≤li≤ri≤n .
In every test case, there are two integers
The second line contains
Then there are
Output
Your program should print T lines : the i -th of these denotes the answer of the i -th case.
If the answer is an integer, just print a single interger, otherwise print an irreducible fraction likep/q .
If the answer is an integer, just print a single interger, otherwise print an irreducible fraction like
Sample Input
13 41 1 52 31 33 31 1
Sample Output
5/4
Source
BestCoder Round #52 (div.2)
问题描述
Victor有n个玩具,编号为1到n,其中编号为i的玩具有wi点有趣值。Victor对数字特别敏感,他在头脑中生成了m个区间,其中第i个区间为[li,ri],并随机选取了3个互不相同的数i,j,k(1≤i<j<k≤m),将所有满足max(li,lj,lk)≤x≤min(ri,rj,rk)的编号为x的玩具取出,他想知道他取出的玩具的有趣值之和的期望是多少,你能帮帮他吗?
输入描述
第一行包含一个整数T,表示测试数据的组数。每组测试数据的第一行有两个整数n, m,表示玩具的个数和区间的个数。接下来一行有n个数,其中第i个数为wi。接下来m行,每行两个整数,其中第i行表示li, ri。1≤T≤10。1≤n,m≤50000。1≤wi≤5。1≤li≤ri≤n。
输出描述
每组测试数据输出一行,若答案为整数,则输出一个整数,否则输出形如p/q的最简分数。
输入样例
13 41 1 52 31 33 31 1
输出样例
5/4
解题思路:和的期望 = (∑任意一种挑选方式的和x)/(满足i<=j<=k的挑选方式的总数y) = x / y 。分母y 就是m个数中取3个数的组合C(m,3),即y = m*(m-1)*(m-2)/6 ,对于分子x,如果第i个数出现的区间数是num(简言之就是有num个区间包含第i个数),那么第i个数对分子的贡献值是w[i]*C(num,3)。因此枚举i,累加和。(预处理过程与HDU 5124 line 类似)
代码如下:
#include <cstdio>#include <iostream>#include <cstdlib>#include <cstring>#include <cmath>#include <string>#include <algorithm>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <numeric>#include <iomanip>#include <bitset>#include <sstream>#include <fstream>#include <limits.h>#include <ctime>#define debug "output for debug\n"#define pi (acos(-1.0))#define eps (1e-6)#define inf (1<<28)#define sqr(x) (x) * (x)#define mod 1000000007using namespace std;typedef long long ll;typedef unsigned long long ULL;int w[50005],dp[50005];ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);}int main(){ ll i,j,k,n,m,l,r,t; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d",&n,&m); for(i=1;i<=n;i++) scanf("%I64d",&w[i]); memset(dp,0,sizeof(dp)); for(i=0;i<m;i++) { scanf("%I64d%I64d",&l,&r); dp[l]++; dp[r+1]--; } ll x=0; ll num=0; for(i=1;i<=n;i++) { num+=dp[i]; if(num>=3) x+=num*(num-1)*(num-2)/6*w[i]; } ll y=m*(m-1)*(m-2)/6; //printf("x=%I64d y=%I64d\n",x,y); ll d=gcd(x,y); if(d) { x=x/d; y=y/d; if(y==1) printf("%I64d\n",x); else printf("%I64d/%I64d\n",x,y); } else printf("0\n"); } return 0;}
0 0
- HDU 5419 Victor and Toys ()
- hdu 5419 Victor and Toys
- HDU 5419 Victor and Toys
- hdu 5419 Victor and Toys(暴力+组合)
- hdu 5419 Victor and Toys 线段树成段更新
- hdu 5419 Victor and Toys(期望+差分前缀和)
- 【HDOJ 5419】 Victor and Toys (排列组合)
- HDOJ 5419 Victor and Toys 树状数组
- hdu5419--Victor and Toys(枚举)
- 【hdu5419】Victor and Toys
- BestCoder#52 Victor and Toys
- hdu 5417 Victor and Machine(模拟)
- hdu 5418 Victor and World(dp)
- hdu5419 Victor and Toys 期望,差分前缀和
- hdu 5417 Victor and Machine
- hdu 5417 Victor and Machine
- HDU 5417 Victor and Machine
- HDU-5417Victor and Machine
- Java中用浮点型数据Float和Double进行精确计算时的精度问题
- oracle时间操作
- ajax请求超时处理
- block,inline和inline-block概念和区别
- minMaxLoc用法
- HDU 5419 Victor and Toys ()
- swift常用控件
- 各种排序算法的分析及java实现
- struts OgnlRunTimeException
- android-studio 快捷键
- 探索中国软件的智造之路
- VBA中实现数组排序的多种方法
- request
- 【JS】JavaScript引擎的内部运行机制