• [1553] Beautiful Walls

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • To simplify the building process, XadillaX built some template on the ground. The template is a very big wall and the height of each unit may be different.

    8Mao and Hungar have to choose any part of this wall as their own wall.

    The part(i, j) means the wall between unit(i) and unit(j) with their heights.
    What Hungar thinks a beautiful wall is that the height of each unit is unique.
    Now give you a wall-template, you should tell Hungar that how many ways he can choose to copy his own wall?

  • 输入
  • This problem contains several cases, ends with EOF.
    The first line of each case is one integer N (0 < N ≤ 100000) which indicates the side length (number of units) of the wall-template.
    The second line contains N integers which indicate the height of each unit. (0 < height <= 100000)
  • 输出
  • For each case, you should output the number of ways that Hungar can choose.
  • 样例输入
  • 5
    3 4 5 5 2
    3
    1 2 3
  • 样例输出
  • 9
    6
  • 提示
  • 来源
  • cjl
  • 思路:尺取法,组合数学;
  • 用尺取法取区间,在这个区间内所有的数都不同,那么可行方案数为Cn2,两个区间还会相交,所以还得减去重复算的,比如1 2 3 4 5 3 2 1
  • 前一个区间为[1,5],后一个为[4,1];所以[4,5]相交,所以去重.那么关键是记录相交的地方,我们可以这样,记录上个区间的结束,然后根据尺取法可知道当前
  • 区间的开头,这样就可以算出了。复杂O(n);
  •  1 #include<stdio.h>
    2 #include<algorithm>
    3 #include<iostream>
    4 #include<string.h>
    5 #include<stdlib.h>
    6 #include<math.h>
    7 using namespace std;
    8 typedef long long LL;
    9 int uu[1000005];
    10 int flag[1000005];
    11 int yy[1000005];
    12 int main(void)
    13 { int i,j,k;
    14 while(scanf("%d",&k)!=EOF)
    15 { memset(flag,0,sizeof(flag));
    16 memset(yy,0,sizeof(yy));
    17 for(i=0;i<k;i++)
    18 {
    19 scanf("%d",&uu[i]);
    20 }
    21 LL ans=1;long long sum=0;int vv=0;
    22 int l=0;int r=0;flag[uu[0]]=1;int kp=0;
    23 while(l<=r+1&&r<k-1)
    24 {
    25 while(r<k-1)
    26 {
    27 r++;
    28 if(flag[uu[r]]==1)
    29 {LL yyk=0;
    30 if(ans>0&&!yy[r-1])
    31 {
    32 yy[r-1]=1;
    33 if(kp>l)
    34 {
    35 yyk=kp-l+1;
    36 }
    37 sum+=ans*(ans-1)/2;if(yyk>0)sum-=yyk*(yyk-1)/2;
    38 kp=r-1;
    39 }ans++;flag[uu[r]]++;break;
    40 }
    41 else if(!flag[uu[r]])
    42 {
    43 ans++;flag[uu[r]]++;
    44 }
    45 }
    46 if(r==k-1&&flag[uu[r]]==1)
    47 break;
    48 else {flag[uu[r]]--;flag[uu[l]]--;ans-=2;r--;l++;}
    49 }
    50 if(ans>0){sum+=ans*(ans-1)/2;sum+=(LL)k;LL yyk=kp-l+1;if(yyk>0){sum-=yyk*(yyk-1)/2;}}
    51 printf("%I64d\n",sum);
    52 }
    53 return 0;
    54 }

Beautiful Walls的更多相关文章

  1. HDU 6187 Destroy Walls

    Destroy Walls Long times ago, there are beautiful historic walls in the city. These walls divide the ...

  2. HDU 6187 Destroy Walls (思维,最大生成树)

    HDU 6187 Destroy Walls (思维,最大生成树) Destroy Walls *Time Limit: 8000/4000 MS (Java/Others) Memory Limit ...

  3. 使用Beautiful Soup编写一个爬虫 系列随笔汇总

    这几篇博文只是为了记录学习Beautiful Soup的过程,不仅方便自己以后查看,也许能帮到同样在学习这个技术的朋友.通过学习Beautiful Soup基础知识 完成了一个简单的爬虫服务:从all ...

  4. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(1): 基础知识Beautiful Soup

    开始学习网络数据挖掘方面的知识,首先从Beautiful Soup入手(Beautiful Soup是一个Python库,功能是从HTML和XML中解析数据),打算以三篇博文纪录学习Beautiful ...

  5. Python爬虫学习(11):Beautiful Soup的使用

    之前我们从网页中提取重要信息主要是通过自己编写正则表达式完成的,但是如果你觉得正则表达式很好写的话,那你估计不是地球人了,而且很容易出问题.下边要介绍的Beautiful Soup就可以帮你简化这些操 ...

  6. 推荐一些python Beautiful Soup学习网址

    前言:这几天忙着写分析报告,实在没精力去研究django,虽然抽时间去看了几遍中文文档,还是等实际实践后写几篇操作文章吧! 正文:以下是本人前段时间学习bs4库找的一些网址,在学习的可以参考下,有点多 ...

  7. 数位DP CF 55D Beautiful numbers

    题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...

  8. 错误 You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work

    Win  10    下python3.6 使用Beautiful Soup  4错误 You are trying to run the Python 2 version of Beautiful ...

  9. hihoCoder 1425 : What a Beautiful Lake(美丽滴湖)

    hihoCoder #1425 : What a Beautiful Lake(美丽滴湖) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 ...

随机推荐

  1. adverb

    An adverb is a word or an expression that modifies a verb, adjective, another adverb, determiner [限定 ...

  2. acknowledge

    accord+knowledge. accord好几个意思,knowledge不遑多让,We gotta acknowledge the word acknowledge has many meani ...

  3. Express中间件原理详解

    前言 Express和Koa是目前最主流的基于node的web开发框架,他们的开发者是同一班人马.貌似现在Koa更加流行,但是仍然有大量的项目在使用Express,所以我想通过这篇文章说说Expres ...

  4. 关于stm32不常用的中断,如何添加, 比如timer10 timer11等

    首先可以从keil中找到 比如找到定时器11的溢出中断,如上图是26 然后,配置定时器11 溢出中断的时候,我就在:下面填上这个变量. 之后要写中断服务函数,也就是发生中断后要跳转到的函数. 需要知道 ...

  5. 栈常考应用之括号匹(C++)

    思路在注释里.还是使用链栈的API,为啥使用链栈呢,因为喜欢链栈. //header.h #pragma once #include<iostream> using namespace s ...

  6. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(六)-FatFs使用的思路介绍

    [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...

  7. 银联acp手机支付总结

    总结: 1.手机调用后台服务端接口,获取银联返回的流水号tn 银联支付是请求后台,后台向银联下单,返回交易流水号,然后返回给用户,用户通过这个交易流水号,向银联发送请求,获取订单信息,然后再填写银行卡 ...

  8. Android 图片框架

    1.图片框架:Picasso.Glide.Fresco 2.介绍: picasso:和Square的网络库能发挥最大作用,因为Picasso可以选择将网络请求的缓存部分交给了okhttp实现 Glid ...

  9. 编译安装nginx 1.16

    准备源码包,并解压,创建nginx用户 [root@slave-master ~]# tar xf nginx-1.16.0.tar.gz [root@slave-master ~]# useradd ...

  10. Linux提取命令grep 有这一篇就够了

    grep作为linux中使用频率非常高的一个命令,和cut命令一样都是管道命令中的一员.并且其功能也是对一行数据进行分析,从分析的数据中取出我们想要的数据.也就是相当于一个检索的功能.当然了,grep ...