2038: [2009国家集训队]小Z的袜子(hose)
2016-08-10 15:35
295 查看
2038: [2009国家集训队]小Z的袜子(hose)
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7026 Solved: 3222
[Submit][Status][Discuss]
Description
作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
你的任务便是告诉小Z,他有多大的概率抽到两只颜色相同的袜子。当然,小Z希望这个概率尽量高,所以他可能会询问多个(L,R)以方便自己选择。
Input
输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。Output
包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)Sample Input
6 41 2 3 3 3 2
2 6
1 3
3 5
1 6
Sample Output
2/50/1
1/1
4/15
【样例解释】
询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。
询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。
询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。
注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。
【数据规模和约定】
30%的数据中 N,M ≤ 5000;
60%的数据中 N,M ≤ 25000;
100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。
HINT
Source
版权所有者:莫涛[Submit][Status][Discuss]
莫队,,为什么bzoj跑得辣么快?!
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<bitset> #include<algorithm> #include<cstring> #include<map> #include<stack> #include<set> #include<cmath> #include<ext/pb_ds/priority_queue.hpp> using namespace std; const int maxn = 5E4 + 10; struct data{ int l,r,pos,flag; data(int _l = 0,int _r = 0,int _pos = 0,int _flag = 0) { l = _l; r = _r; pos = _pos;flag = _flag; } bool operator < (const data &b) const { if (flag < b.flag) return 1; if (flag > b.flag) return 0; return r < b.r; } }Q[maxn]; int n,m,cnt,Sqrt,num[maxn],co[maxn],vis[maxn],C[maxn][3],A[maxn],B[maxn]; int gcd(int x,int y) { return x % y == 0?y:gcd(y,x%y); } int main() { #ifdef DMC freopen("DMC.txt","r",stdin); #endif C[0][0] = 1; for (int i = 1; i < maxn; i++) { C[i][0] = 1; for (int j = 1; j < 3; j++) C[i][j] = C[i-1][j] + C[i-1][j-1]; } cin >> n >> m; Sqrt = sqrt(n); for (int i = 1; i <= n; i++) scanf("%d",&num[i]); for (int i = 1; i <= m; i++) { int L,R; scanf("%d%d",&L,&R); int pos = L/Sqrt; if (L % Sqrt != 0) ++pos; Q[i] = data(L,R,i,pos); } sort(Q + 1,Q + m + 1); int L,R,tot; R = tot = 0; L = 1; for (int i = 1; i <= m; i++) { if (Q[i].r > R) { for (int j = R + 1; j <= Q[i].r; j++) { tot += co[num[j]]; ++co[num[j]]; } R = Q[i].r; } else if (Q[i].r < R) { for (int j = R; j > Q[i].r; j--) { --co[num[j]]; tot -= co[num[j]]; } R = Q[i].r; } if (Q[i].l > L) { for (int j = L; j < Q[i].l; j++) { --co[num[j]]; tot -= co[num[j]]; } L = Q[i].l; } else if (Q[i].l < L) { for (int j = Q[i].l; j < L; j++) { tot += co[num[j]]; ++co[num[j]]; } L = Q[i].l; } int GCD = gcd(tot,C[R - L + 1][2]); A[Q[i].pos] = tot/GCD; B[Q[i].pos] = C[R - L + 1][2]/GCD; } for (int i = 1; i <= m; i++) printf("%d/%d\n",A[i],B[i]); return 0; }
相关文章推荐
- MySQL常用查询语句
- 瀑布流JavaScript
- 1540 银河英雄传说
- 华为OJ——字串的连接最长路径查找
- 【poj2546】 Circular Area
- 逻辑理论部分
- Android Content Provider详解及示例代码
- 微信公众平台开发:进阶篇(Web App开发入门)
- UI测试
- 粒子系统(二)
- 编辑距离算法
- Sqlmap Tamper大全
- SAP中变式的删除
- 正则表达式 正向前瞻
- CODEFORCES 430E Guess the Tree <dfs搜索建树>
- MemcachedClient 使用说明
- spring中关于<context:component-scan>的使用说明
- Tomcat多实例 - 单机
- ZTree节点的常用操作
- Android跳转其他app指定activity