HDU 4638 2013多校联合第4场 G-Group
来源:互联网 发布:漳州卓知职业培训学校 编辑:程序博客网 时间:2024/06/10 05:00
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=4638
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 747 Accepted Submission(s): 405
Problem Description
There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value of an interval is sum of these value of groups. The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.
Input
First line is T indicate the case number.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
Output
For every query output a number indicate there should be how many group so that the sum of value is max.
Sample Input
15 23 1 2 5 41 52 4
Sample Output
12
题意:任意一个1~n的排列,每次询问一段区间,问该区共有多少组包含连续数字的数组。
思路:此题如果将询问按照右端点从小到大排列,然后将所有的连续的数字组数合并到最右的数字,再用树状数组来维护,就非常简单了。
AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAX 100010int n, m, c[MAX], a[MAX], pos[MAX];struct L {int l, r, id;int ans;bool operator <(L othr) const {return id < othr.id;}} query[MAX];int cmp(L x, L y) {return x.r < y.r;}inline int lowbit(int x) {return x & (-x);}void add(int i, int val) {while (i <= MAX) {c[i] += val;i += lowbit(i);}}int getsum(int i) {int s = 0;while (i >= 1) {s += c[i];i -= lowbit(i);}return s;}int main() {int T;scanf("%d", &T);while (T--) {memset(c, 0, sizeof(c));scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", a + i);pos[a[i]] = i; //保存每个数字出现的地址}for (int i = 0; i < m; i++) {scanf("%d%d", &query[i].l, &query[i].r);query[i].id = i;}sort(query, query + m, cmp); //按照区间的右端点排列int cur = 0;for (int i = 1; i <= n; i++) {add(i, 1); //将该位置取1if (a[i] > 1 && pos[a[i] - 1] < i) add(pos[a[i] - 1], -1); if (a[i] < n && pos[a[i] + 1] < i) add(pos[a[i] + 1], -1);//如果之前有一个比a[i]相邻的数,则将该位1的大小转移到右边i的位置while (query[cur].r == i && cur < m) { //如果刚好更新到询问的节点//cout<<i<<' '<<getsum(query[p].r)<<' '<<getsum(query[p].l)<<endl;query[cur].ans = getsum(query[cur].r) - getsum(query[cur].l - 1);cur++;}}sort(query, query + m);//按照原来的顺序排序输出for (int i = 0; i < m; i++) {printf("%d\n", query[i].ans);}}}
- HDU 4638 2013多校联合第4场 G-Group
- 2013 多校联合4 1007 Group(hdu 4638)
- HDU 4632 2013多校联合第4场 A - Palindrome subsequence
- HDU 3521 2010多校联合第9场 数学
- hdu 4638 Group 多校第四场
- 2013 多校第四场 hdu 4638 Group
- 2013暑期多校联合训练\第三场\Problem G
- hdu 4361 2013多校联合训练第3场最后一题
- HDU 4651 2013多校联合第5场 Partition 数论
- HDU 4655 2013多校联合赛第6场 Cut Pieces
- HDU 4658 2013 多校联合赛第6场 Integer Partition
- hdu 4602 partition 2013多校联合训练第一场
- 2013年HDU多校联合第三场解题报告
- hdu 4379 The More The Better 多校联合赛事第8场
- 2014多校联合第9场1011题||hdu 4970 树状数组
- 2014多校联合第9场1002||hdu 4691 质因子
- 2014多校联合第9场1006||hdu 4965 矩阵乘法和快速幂
- 2017多校联合第8场1008/hdu 6140Hybrid Crystals(思维)
- 通配符
- java中八种基本数据类型以及它们的封装类,String类型的一些理解
- 数据结构--二叉树的存储
- 银行业务调度系统
- hdu 3308 线段树区间合并
- HDU 4638 2013多校联合第4场 G-Group
- Assignment 9: Computational Geometry
- Android系统信息获取 之五:系统语言信息(续)-语言地区和简写对照表
- 数据业务支撑系统
- 【三句简单却值得牢记的话】
- UVA 10790 (13.08.06)
- UVA 624 - CD
- 用到的与sql相关的总结
- ASIHttpRequest详解