zoukankan      html  css  js  c++  java
  • HDU 4638Group (莫队)

    Group

    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

    题目大意:

    多组数据

    给出n和m,a[i]为1到n的不重复数

    询问m次

    问一段区间内,连续的一段可分为一组(可以打乱顺序),问至少分多少组,

    莫队板子题。

    这里纠正一下莫队细节

    先加再删,不要先删再加

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #define maxn 100100
    using namespace std;
    int n,m,zz,k;
    int a[maxn];
    int belong[maxn];
    int vis[maxn];
    int ans;
    inline int read()
    {
        int x=0,f=1;char s=getchar();
        while('0'>s||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while('0'<=s&&s<='9') {
            x=x*10+s-'0';
            s=getchar();
        }
        return x*f;
    }
    struct edge {
        int x,y,id;
        int ans;
        bool operator < (const edge &a) const {
            return belong[x]==belong[a.x] ? y<a.y : x<a.x;
        }
    } q[maxn];
    inline int cmp(const edge &a,const edge &b) {
        return a.id<b.id;
    }
    inline void Add(int x) {
        int dsr=vis[x+1]+vis[x-1];
        dsr==0 ? ++ans : dsr==2?--ans:ans;
        ++vis[x];
    }
    inline void Delet(int x) {
        int dsr=vis[x+1]+vis[x-1];
        dsr==0? --ans : dsr==2?++ans:ans;
        --vis[x];
    }
    
    int main() {
        zz=read();
        while(zz--) {
            memset(a,0,sizeof(a));
            memset(belong,0,sizeof(belong));
            memset(q,0,sizeof(q));
            memset(vis,0,sizeof(vis));
            ans=0;
            n=read();
            m=read();
            k=sqrt(n);
            for(int i=1; i<=n; ++i) {
                a[i]=read();
                belong[i]=(i-1)/k+1;
            }
            for(int i=1,x,y; i<=m; ++i) {
                q[i].x=read();
                q[i].y=read();
                q[i].id=i;
            }
            sort(q+1,q+1+m);
            for(register int i=1,l=1,r=0; i<=m; ++i) {
                register int x=q[i].x,y=q[i].y;
                while(r < y) Add(a[++r]);
                while(r > y) Delet(a[r--]);
                while(l < x) Delet(a[l++]);
                while(l > x) Add(a[--l]);
                q[i].ans=ans;
            }
            sort(q+1,q+1+m,cmp);
            for(int i=1; i<=m; ++i)
                printf("%d
    ",q[i].ans);
        }
        return 0;
    }
    
  • 相关阅读:
    LINQ分组排序后获取每组第一条记录
    String 中的Trim
    C# Switch优雅写法
    C# 输入指定日期获取当前年的第一天 、当前年的最后天、某月的第一天 、某月的最后一天
    快捷方式 ABP——切换MySQL数据库
    新建立git分支,之后将分支合并到master上
    C# Contains()、 == 和Equals() 比较
    使用TimeSpan 日期与时间拼接
    ActiveReports报表行号
    iOS基础(八)——最新更新方法,每天只提示一次
  • 原文地址:https://www.cnblogs.com/dsrdsr/p/9343307.html
Copyright © 2011-2022 走看看