Light OJ 1339 Strongest Community(分块暴力)
来源:互联网 发布:U盘品牌 知乎 编辑:程序博客网 时间:2024/05/16 07:37
In a strange city, houses are built in a straight line one after another. There are several communities in the city. Each community consists of some consecutivehouses such that every house belongs to exactly one community. The houses are numbered from 1 to n, and the communities are numbered from 1 to c.
Now some inspectors want to find the strongest community considering all houses from i to j. A community is strongest if maximum houses in the range belong to this community. So, there can be more than one strongest community in the range. So, they want to know the number of houses that belong to the strongest community. That's why they are seeking your help.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing three integers n (1 ≤ n ≤ 105), c (1 ≤ c ≤ n) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers (each between 1 and c) denoting the communities the houses belong to. You can assume that the input follows the restrictions given above, and there are exactly c communities.
Each of the next q lines contains two integers i and j (1 ≤ i ≤ j ≤ n) denoting that the inspectors are asking for the result considering houses from i to j(inclusive).
Output
For each case, print the case number in a single line. Each of the next q lines should contain the number of houses that belong to the strongest community considering houses from i to j. The result should be listed in the same order as they are given in input.
Sample Input
Output for Sample Input
2
10 3 4
1 1 1 3 3 3 3 2 2 2
1 5
1 6
1 7
7 9
3 3 1
3 2 1
1 1
Case 1:
3
3
4
2
Case 2:
1
Note
Dataset is huge, use faster
题意:询问区间出现次数最多的数字出现次数
分析:线段树不会做,分块暴力吧,我们统计区间中数子出现次数
此时要统计两个信息:Add操作:增加一个数字会使出现频率变化,相应要修改最值
Sub操作:删除一个数字,如果删除数字后还有其他数达到相同水平,才改变最值
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=1e5+100;int pos[maxn],a[maxn];int h[maxn],num[maxn];int ans[maxn];struct node{ int l,r; int id;}q[maxn];int t,n,c,m;int sum;int cmp(node l1,node l2){ if(pos[l1.l]==pos[l2.l]) return l1.r<l2.r; return pos[l1.l]>pos[l2.l];}void Add(int x){ int val=a[x]; h[num[val]]--; num[val]++; h[num[val]]++; if(num[val]>sum) sum=num[val];}void Sub(int x){ int val=a[x]; h[num[val]]--; num[val]--; h[num[val]]++; if(sum==num[val]+1&&!h[num[val]+1]) sum=num[val];}int main(){ int x,y; int cas=1; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&c,&m); CLEAR(h,0); CLEAR(num,0); int block=sqrt(n*1.0+0.5); REPF(i,1,n) { scanf("%d",&a[i]); pos[i]=(i-1)/block+1; } REP(i,m) { scanf("%d%d",&x,&y); q[i].l=x;q[i].r=y; q[i].id=i; } sort(q,q+m,cmp); sum=0;int L=1,R=0; printf("Case %d:\n",cas++); for(int i=0;i<m;i++) { int l=q[i].l; int r=q[i].r; while(R>r) Sub(R--); while(R<r) Add(++R); while(L>l) Add(--L); while(L<l) Sub(L++); ans[q[i].id]=sum; } for(int i=0;i<m;i++) printf("%d\n",ans[i]); } return 0;}
- Light OJ 1339 Strongest Community(分块暴力)
- Light OJ 1188 Fast Queries(分块暴力)
- lightoj 1339 Strongest Community 线段树
- light oj 1337--DFS--分块
- Light OJ 1049 - One Way Roads (暴力 or DFS)
- Light OJ:1307 Counting Triangles(二分+暴力枚举)
- 分块(有理有据的暴力)
- light oj(简化复杂度)
- WHU 1578 Fishing Dream(分块暴力)
- WHU 1551 Pairs(分块暴力)
- Lightoj 1148 - Mad Counting (暴力分块)
- 【BZOJ 2120】 数颜色 (分块,暴力)
- 【BZOJ1257】余数之和(数论分块,暴力)
- light oj
- light oj
- Light OJ
- Light OJ
- 玲珑学院OJ 1129 喵哈哈村的战斗魔法师丶坏坏い月【暴力分块】
- uva 361 - Cops and Robbers(凸包)
- 求单链表中倒数第k个结点
- python学习笔记(一)
- Android 控件沿贝塞尔曲线运动(下)———仿微信掉表情包
- uva 1411 Ants (权值和最小的完美匹配---KM算法)
- Light OJ 1339 Strongest Community(分块暴力)
- 杭电 1874 畅通工程续(Dijkstra)
- 操作系统 时间片轮转调度算法
- python_学习笔记0817
- python时间日期操作
- 京东收藏夹
- javascript中对各种事件处理程序的个人总结
- hdu1874畅通工程续【最短路dijkstra&&SPFA&&floyd】
- 解决YUM无法正常工作