hackerrank Week of Code 34 Same Occurrence

之前也想到了利用前缀数组,记录每一位的0~i的值,但是这样效率并不高。

题解的方法是每出现一次这个数字,记录一下下标,然后通过求区间的交,这样优化就不会超时了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define pb push_back
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define cls(name,x) memset(name,x,sizeof(name))//0或-1
#define fs first
#define sc second
#define mp make_pair
#define L(x) (1<<x)
using namespace std;
const int inf=1e9+10;
const ll llinf=1e16+10;
const int maxn=8e3+10;
const int maxm=2e5+10;
const int mod=1e9+7;
int n,q;
int ans[maxn][maxn];
vector<int> v[maxn];
map<int,int> mapp;
int main()
{
    //freopen("in.txt","r",stdin);
    cls(ans,-1);
    scanf("%d %d",&n,&q);
    for(int i=1;i<=n;i++)
        v[i].pb(0);
    for(int i=1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        if(mapp[t]==0)
        mapp[t]=i;
        v[mapp[t]].pb(i);
    }
    map<int,int> c;
    while(q--)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        if(ans[mapp[x]][mapp[y]]!=-1)
        {
            printf("%d\n",ans[mapp[x]][mapp[y]]);
            continue;
        }
        if(x==y||(mapp[x]==0&&mapp[y]==0))
        {
            ans[mapp[x]][mapp[y]]=n*(n+1)/2;
        }
        else if(mapp[x]==0||mapp[y]==0)
        {
            int t=(mapp[x]!=0?mapp[x]:mapp[y]);
            int sum=0,len;
            for(int i=1;i<v[t].size();i++)
            {
                int len=v[t][i]-v[t][i-1]-1;
                sum+=len*(len+1)/2;
            }
            len=n-v[t][v[t].size()-1];
            sum+=len*(len+1)/2;
            ans[mapp[x]][mapp[y]]=sum;
        }
        else
        {
            c.clear();
            int sum=0;
            int t1=mapp[x],t2=mapp[y];
            /*for(int i=0;i<v[t1].size();i++)
                printf("%d ",v[t1][i]);
            printf("\n");
            for(int i=0;i<v[t2].size();i++)
                printf("%d ",v[t2][i]);
            printf("\n");*/
            int xl,xr,idx1=0;
            int yl,yr,idx2=0;
            while(idx1<v[t1].size()&&idx2<v[t2].size())
            {
                xl=v[t1][idx1];
                if(idx1+1>=v[t1].size())
                    xr=n;
                else xr=v[t1][idx1+1]-1;

                yl=v[t2][idx2];
                if(idx2+1>=v[t2].size())
                    yr=n;
                else yr=v[t2][idx2+1]-1;

                //printf("[%d %d:%d] [%d %d:%d]\n",xl,xr,idx1,yl,yr,idx2);
                int len=(min(xr,yr)-max(xl,yl)+1);
                sum+=c[idx1-idx2]*len+len*(len-1)/2;
                c[idx1-idx2]+=len;
                if(xr<yr) idx1++;
                else if(xr>yr) idx2++;
                else {idx1++;idx2++;}
            }
            ans[mapp[x]][mapp[y]]=sum;
        }
        printf("%d\n",ans[mapp[x]][mapp[y]]);
    }
    return 0;
}
时间: 2024-08-10 03:11:13

hackerrank Week of Code 34 Same Occurrence的相关文章

【HackerRank Week of Code 31】Colliding Circles

https://www.hackerrank.com/contests/w31/challenges/colliding-circles/problem 设E(n)为序列长度为n时的期望值. \[ \begin{aligned} E(n-1)=&E(n)+\frac1{n\choose2}\sum_{0\leq i<j\leq n}2r_ir_j\=&E(n)+\frac1{n\choose2}\left[\left(\sum r_i\right)^2-\sum r_i^2\righ

hackerrank [Week of Code 33] Bonnie and Clyde

任意门 题意:给一个图,每次询问给三个点a,b,c,问是否存在一条从a到c,一条b到c的路径除c外无交点. 双连通分量缩点建出圆方树是必须的,然后我们需要判断c是否在a到b的路径上,或者c的某个相邻的方点(新建的节点)在a到b的路径上.最后这玩意判了很久就是一直不对,去膜了ccz代码--哦,lca(a,b),lca(a,c),lca(b,c)只有俩不同取值,异或一下就得到多余的一个,然后就很好判了. #include<cstdio> #include<algorithm> #def

【枚举约数】HackerRank - Week of Code 26 - Satisfactory Pairs

题意:给你一个正整数n,问你存在多少个正整数对a,b(a<b),满足条件:存在正整数x,y,使得ax+by=n. 就预处理出n以内所有数的约数,然后暴力枚举a,暴力枚举x,然后枚举n-ax的所有约数,判重,统计答案即可. #include<cstdio> #include<vector> #include<algorithm> using namespace std; typedef vector<int>::iterator ITER; vector

Hackerrank: Week of Code 36

Cut a Strip 题目简述:给定$n \times m$的矩阵$a[][]$,要求选择一个$x \times 1(1 \leq x \leq k)$的(连续)子矩阵并清零后,找到最大和的(连续)子矩阵. 数据范围:$1 \leq n, m, k \leq 380$. 题解: 子矩阵可以用四个参数表示$(x_1, y_1, x_2, y_2)$,其中$(x_1, y_1)$是其左上角,$(x_2, y_2)$是其右上角. 我们枚举子矩阵的$y_1$和$y_2(1 \leq y_1 \leq

关于java.io.IOException: Server returned HTTP response code: 400 for URL报错和string.getBytes()字符集

400 请求出错:由于语法格式有误,服务器无法理解此请求总论:这种错误应该会有很多原因,这里指出的是因为字符集编码的原因导致400,主要代码:向服务器发送请求传输json参数用的是out.write(json.getBytes())(读取的是操作系统的字符集,如果操作系统与部署项目的服务器不同则报错);改为out.writeChars(json);或out.write(json.getBytes(服务器编码))即可.如下代码16行 1 //创建连接 2 URL url = new URL(u);

Callback Function

typedef void (*callbackFun)(int a, int b);struct exm { int type; callbackFun fun;}; A pointer is a special kind of variable that holds the address of another variable. The same concept applies to function pointers, except that instead of pointing to

linux内核分析之内存管理

1.struct page 1 /* Each physical page in the system has a struct page associated with 2 * it to keep track of whatever it is we are using the page for at the 3 * moment. Note that we have no way to track which tasks are using 4 * a page, though if it

MySQL Error Codes MYSQL的错误代码

OS error code 1: Operation not permitted OS error code 2: No such file or directory OS error code 3: No such process OS error code 4: Interrupted system call OS error code 5: Input/output error OS error code 6: No such device or address OS error code

photoshop cc 版本安装失败解决办法

好久没有碰ps,看了下在ps版本都到cc了.忍不住也想尝试最新版本,但是安装出现了很多问题,导致我花了很多时间才搞定,现在分享给大家几点经验吧. Exit Code: 34 Please see specific errors below for troubleshooting. For example, ERROR: -------------------------------------- Summary -------------------------------------- - 1