HDOJ 4638 Group
2014-04-03 21:19
281 查看
求一段区间内有几段连续的数。。。
考虑从左开始往右扫:
对于数a 如果a+1和a-1都没有那么区间段数加1;如果a+1或a-1中的一个已经在被扫过了,那么区间段数没有增加;
如果a+1和a-1都在,正好把两个数连起来区间段数-1。。
变化总和就是。。。最终的段数。
将询问离线按L排序处理,删除数a的时候,如果与a相邻的数还在后面,那么对应的变化值+1。。。。
数状数组维护即可
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1183 Accepted Submission(s): 637
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].
Output
For every query output a number indicate there should be how many group so that the sum of value is max.
Sample Input
1
5 2
3 1 2 5 4
1 5
2 4
Sample Output
1
2
Source
2013 Multi-University Training Contest 4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int maxn=110000;
int n,m,a[maxn],tr[maxn],ans[maxn],ntop[maxn];
struct Question
{
int L,R,id;
}Q[maxn];
bool cmp(Question a,Question b)
{
if(a.L!=b.L) return a.L<b.L;
return a.R<b.R;
}
int lowbit(int x)
{
return x&(-x);
}
void ADD(int p,int v)
{
for(int i=p;i<maxn;i+=lowbit(i))
tr[i]+=v;
}
int SUM(int p)
{
int sum=0;
for(int i=p;i;i-=lowbit(i))
sum+=tr[i];
return sum;
}
int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
ntop[a[i]]=i;
}
for(int i=0;i<m;i++)
{
Q[i].id=i;
scanf("%d%d",&Q[i].L,&Q[i].R);
}
sort(Q,Q+m,cmp);
memset(tr,0,sizeof(tr));
set<int> st;
///一遍扫过去
for(int i=1;i<=n;i++)
{
int temp=0,left=st.count(a[i]-1),right=st.count(a[i]+1);
st.insert(a[i]);
if(left&&right) temp=-1;
if(left==0&&right==0) temp=1;
ADD(i,temp);
}
int cur=1;
for(int i=0;i<m;i++)
{
while(cur<Q[i].L)
{
if(st.count(a[cur]-1)) ADD(ntop[a[cur]-1],1);
if(st.count(a[cur]+1)) ADD(ntop[a[cur]+1],1);
st.erase(a[cur]);
cur++;
}
ans[Q[i].id]=SUM(Q[i].R)-SUM(Q[i].L-1);
}
for(int i=0;i<m;i++) printf("%d\n",ans[i]);
}
return 0;
}
考虑从左开始往右扫:
对于数a 如果a+1和a-1都没有那么区间段数加1;如果a+1或a-1中的一个已经在被扫过了,那么区间段数没有增加;
如果a+1和a-1都在,正好把两个数连起来区间段数-1。。
变化总和就是。。。最终的段数。
将询问离线按L排序处理,删除数a的时候,如果与a相邻的数还在后面,那么对应的变化值+1。。。。
数状数组维护即可
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1183 Accepted Submission(s): 637
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].
Output
For every query output a number indicate there should be how many group so that the sum of value is max.
Sample Input
1
5 2
3 1 2 5 4
1 5
2 4
Sample Output
1
2
Source
2013 Multi-University Training Contest 4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int maxn=110000;
int n,m,a[maxn],tr[maxn],ans[maxn],ntop[maxn];
struct Question
{
int L,R,id;
}Q[maxn];
bool cmp(Question a,Question b)
{
if(a.L!=b.L) return a.L<b.L;
return a.R<b.R;
}
int lowbit(int x)
{
return x&(-x);
}
void ADD(int p,int v)
{
for(int i=p;i<maxn;i+=lowbit(i))
tr[i]+=v;
}
int SUM(int p)
{
int sum=0;
for(int i=p;i;i-=lowbit(i))
sum+=tr[i];
return sum;
}
int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
ntop[a[i]]=i;
}
for(int i=0;i<m;i++)
{
Q[i].id=i;
scanf("%d%d",&Q[i].L,&Q[i].R);
}
sort(Q,Q+m,cmp);
memset(tr,0,sizeof(tr));
set<int> st;
///一遍扫过去
for(int i=1;i<=n;i++)
{
int temp=0,left=st.count(a[i]-1),right=st.count(a[i]+1);
st.insert(a[i]);
if(left&&right) temp=-1;
if(left==0&&right==0) temp=1;
ADD(i,temp);
}
int cur=1;
for(int i=0;i<m;i++)
{
while(cur<Q[i].L)
{
if(st.count(a[cur]-1)) ADD(ntop[a[cur]-1],1);
if(st.count(a[cur]+1)) ADD(ntop[a[cur]+1],1);
st.erase(a[cur]);
cur++;
}
ans[Q[i].id]=SUM(Q[i].R)-SUM(Q[i].L-1);
}
for(int i=0;i<m;i++) printf("%d\n",ans[i]);
}
return 0;
}
相关文章推荐
- c#遍历一个文件夹下的所有文件包括子文件夹
- sqlite操作实例
- 8.GUI设计概述 java
- scala-school_collections
- 文件I/O例子
- 我的vim插件
- [翻译] SWTableViewCell
- 自设chrome默认滚动条样式
- 使用Eclipse调试Android Native Application---cocos2d-x + Eclipse + Android + ndk
- Linux权限小记
- CSS布局
- cabs
- javascript中typeof的用法
- 冒泡排序
- Java多线程编程
- SSH框架的简单学习—Structs学习
- C#调用Web Service时的身份验证
- 第7章: jQuery插件的使用和写法---目录
- oracle连接函数
- hadoop集群的配置