1636: Pascal山脉

时间限制: 1 Sec  内存限制: 128 MB
提交: 51  解决: 15
[提交][状态][讨论版]

题目描述

小卡卡顺着老者所指的方向,来到了Pascal神峰的顶峰。老者告诉小卡卡,Pascal山脉有很多座山, 都排在一条直线上,每座山都有不同的高度。Pascal山的山顶有一个神奇的洞穴,进入这个洞穴后,你将会到达这座山前方的另一座山,更加神奇的是,你到达的山一定比他所在的山高度要小。而Pascal圣地最大的宝藏就藏在某一座Pascal山上的洞穴中,这个洞穴的特点是它有一道石门封闭着。小卡卡很想知道进入每座山的洞穴后,他所到达的不同的山会有多少种可能。

输入

第一行一个整数n,表示山的个数.(1<=n<=20000) 第二行有n个整数,从前到后给出每座山的高度。另外两座山可以有相同的高度. (1<=每座山的高度<=maxlongint)

输出

共一行n个整数,互相以一个空格分隔。.第i个整数表示他进入第i号山的洞穴后能够到达的不同的山的个数.

样例输入

5
1 2 3 4 5

样例输出

0 1 2 3 4

提示

前10点n<=20000;

第11点n=35000;

第12点n=50000;

第13点n=100000;

考试时,建议:

if n<=13000 then begin

算法1:O(n方)的模拟算法,n方/2次运算量。

else begin

算法2或算法3

end;

来源

分析:

数状数组

分析:离散化+树状数组

【参考程序】:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
    
int x,y;
} a[200001];
int b[200001],c[200001];
int n,len;
int cmp(const void *s,const void *t)
{
    node i=*(node *)s,j=*(node *)t;
    
return i.x-j.x;
}
int lowbit(int x)
{
    
return x^(x&(x-1));//x&(-x)
}
void modify(int x)
{
    
while (x<=len)
    {
        c[x]++;
        x+=lowbit(x);
    }
}
int getsum(int x)
{
    
int s=0;
    
while (x)
    {
        s+=c[x];
        x-=lowbit(x);
    }
    
return s;
}
int main()
{
    
//freopen("a1.in","r",stdin);
    //freopen("a1.out","w",stdout);

    while (scanf("%d",&n)!=EOF)
    {
        
for (int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].x);
            a[i].y=i;
        }
        qsort(a+1,n,
sizeof(node),cmp);
        a[0].x=-1;
        len=0;
        
for (int i=1;i<=n;i++)
        {
            
if (a[i].x!=a[i-1].x) len++;
            b[a[i].y]=len;
        }
        memset(c,0,
sizeof(c));
        
for (int i=1;i<=n;i++)
        {
            
int sum=getsum(b[i]-1);
            printf("%d\n",sum);
            modify(b[i]);
        }
    }
    
return 0;
}

5

7 3 6 4 2

1 2 3 4 5

0 0 1 1 0

找比它小个数

排序后

x:7 6 4 3 2

y:1 3 4 2 5

7 3 6 4 2

b[i] 5 2 4 3 1

0 0 1 1 0

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
} a[];
int b[],c[];
int n,len;
//比较规则,按x从大到小排序
int cmp(const void *s,const void *t)
{
node i=*(node *)s,j=*(node *)t;
return i.x-j.x;
}
//lowbit操作
int lowbit(int x)
{
return x^(x&(x-));
}
//修改操作
void modify(int x)
{
while (x<=len)
{
c[x]++;
x+=lowbit(x);
}
}
//取前缀和
int getsum(int x)
{
int s=;
while (x)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main()
{
freopen("in.txt","r",stdin);
//freopen("a1.out","w",stdout);
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].y=i;
}
qsort(a+,n,sizeof(node),cmp);
a[].x=-;
len=;
//离散化
for (int i=;i<=n;i++)
{
if (a[i].x!=a[i-].x) len++;
//这里y等于i
b[a[i].y]=len;
}
for (int i=;i<=n;i++){
cout<<b[i]<<" ";
}
cout<<endl;
memset(c,,sizeof(c));
for (int i=;i<=n;i++)
{
//从大到小排序,找比它小的
int sum=getsum(b[i]-);
printf("%d\n",sum);
modify(b[i]);
for (int i=;i<=n;i++){
cout<<c[i]<<" ";
}
cout<<endl;
}
}
return ;
}

1636: Pascal山脉的更多相关文章

  1. [DP地狱训练]Pascal山脉

    OJ题号:ZHOJ1055 思路:树状数组. 首先将数据离散化,然后用线段树维护小于当前高度的山峰已经出现过的数量. #include<cstdio> #include<cstrin ...

  2. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  5. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  6. Pascal 语言中的关键字及保留字

    absolute //指令(变量) abstract //指令(方法) and //运算符(布尔) array //类型 as //运算符(RTTI) asm //语句 assembler //向后兼 ...

  7. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  8. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

  9. caffe学习笔记(一),ubuntu14.04+GPU (用Pascal VOC2007训练数据,并测试)

    把源代码跑起来了,将实验过程记录如下,用于新手入门. 今天和师兄师姐才跑通,来分享下心得.(预训练网络:ImageNet_model,训练集:PASCAL VOC2007, GPU) 首先,整个tra ...

随机推荐

  1. python基础小点

    变量的命名规则 由字母.下划线.数字组成,且不能以数字开头 不能用关键字作为变量名 最好不要与python内置的一些方法和类名冲突 变量名应尽量简短且具有意义,多个单词之间用下划线连接 注释 #  - ...

  2. C# - *.dll vs *.lib (动态链接库 vs 静态链接库)

    静态库 库(Library)就是一段编译好的二进制代码,加上头文件就可以使用. 静态链接库(Windows 下的*.lib, Linux & Mac 下的 .a).之所以叫做静态,是因为静态库 ...

  3. VS 2019企业版激活码

    Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DF

  4. thinkphp 入口绑定

    入口绑定是指在应用的入口文件中绑定某个模块,甚至还可以绑定某个控制器和操作,用来简化URL地址的访问. 绑定模块 例如,我们定义了一个入口文件admin.php,希望可以直接访问Admin模块,那么我 ...

  5. JavaWeb学习篇之----Servlet过滤器Filter和监听器

    首先来看一下Servlet的过滤器内容: 一.Servlet过滤器的概念: ************************************************************** ...

  6. How to SSH Into Your iPhone

    First, I will explain what SSH is and why we do it. SSH (Secure Shell) allows you to exchange data b ...

  7. LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)

    题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Jav ...

  8. A1095 Cars on Campus (30 分)

    Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...

  9. Linux下rsync的安装及简单使用

    2018-09-25 15:39:04 一.RSYNC安装环境: centos6.5 iptables关闭和selinux为disabled 源码安装:到rsync官网下载rsync源码安装包,上传到 ...

  10. 专题:“find -perm”

    Search for files which have read and write permission for their owner, and group, but which other us ...