2013 多校联合4 1007 Group(hdu 4638)

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): 59    Accepted Submission(s): 28


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.


思路:显然,我们要使得value最大,就要尽量将连续的ID分在一组,所以问题转化为求一个区间中连续ID区间的个数。我们从左往右扫描,依次考虑右端点为i的询问,设dp[l]为区间[l,i]的连续区间个数,po[i]为i出现的位置,若还未出现,则为0,设我们当前考虑的右端点为a[i],首先我们假设a[i]不能和区间[1,i-1]中的任何一个数分到一组,则我们要将dp[1]到dp[i-1]全部加1,然后考虑po[a[i]+1]是否不为0,若不为0则说明a[i]-1已经在前面出现,则我们需要将dp[1]到dp[po[a[i]+1]]全部减一个1,因为a[i]可以和a[i]+1分为一组,则我们之前加的1是多余的。对于a[i]-1的情况同理。以上操作可以由线段树或者树状数组什么的实现,然后再将询问按照右端点从小到大排序,离线处理即可,以下是代码实现。


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#define mid ((t[p].l+t[p].r)>>1)
#define ls (p<<1)
#define rs (ls|1)
#define maxn 100010
using namespace std;
struct tree
{
    int l,r;
    int ans;
}t[maxn<<2];
void pushdown(int p)
{
    if(t[p].ans!=0)
    {
        t[ls].ans+=t[p].ans;
        t[rs].ans+=t[p].ans;
        t[p].ans=0;
    }
}
void build(int p,int l,int r)
{
    t[p].l=l,t[p].r=r,t[p].ans=0;
    if(l==r)
    {
        return;
    }
    build(ls,l,mid);
    build(rs,mid+1,r);
}
void add(int p,int l,int r,int val)
{
    if(l>r)
    return;
    if(t[p].l==l&&t[p].r==r)
    {
        t[p].ans+=val;
        return;
    }
    pushdown(p);
    if(l>mid)
    add(rs,l,r,val);
    else if(r<=mid)
    add(ls,l,r,val);
    else
    {
        add(ls,l,mid,val);
        add(rs,mid+1,r,val);
    }
}
int query(int p,int x)
{
    if(t[p].l==t[p].r)
    {
        return t[p].ans;
    }
    pushdown(p);
    if(x>mid)
    return query(rs,x);
    else
    return query(ls,x);
}
int a[maxn],po[maxn];
struct ask
{
    int l,r;
    int num;
}as[maxn];
bool cmp(ask a,ask b)
{
    return a.r<b.r;
}
int ans[maxn];
int main()
{
   // freopen("dd.txt","r",stdin);
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        int n,m,i;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            po[i]=0;
        }
        po[0]=po[n+1]=0;
        build(1,1,n);
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&as[i].l,&as[i].r);
            as[i].num=i;
        }
        sort(as+1,as+m+1,cmp);
        as[m+1].r=maxn;
        int qnum=1;
        for(i=1;i<=n;i++)
        {
            int x=a[i];
            add(1,1,i-1,1);
            if(po[x-1])
            add(1,1,po[x-1],-1);
            if(po[x+1])
            add(1,1,po[x+1],-1);
            po[a[i]]=i;
            add(1,i,i,1);
            while(as[qnum].r<=i)
            {
                ans[as[qnum].num]=query(1,as[qnum].l);
                qnum++;
            }
            if(qnum>m)
            break;
        }
        for(i=1;i<=m;i++)
        printf("%d\n",ans[i]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值