1636: Pascal山脉
1636: Pascal山脉
时间限制: 1 Sec 内存限制: 128 MB
提交: 51 解决: 15
[提交][状态][讨论版]
题目描述
输入
输出
样例输入
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山脉的更多相关文章
- [DP地狱训练]Pascal山脉
OJ题号:ZHOJ1055 思路:树状数组. 首先将数据离散化,然后用线段树维护小于当前高度的山峰已经出现过的数量. #include<cstdio> #include<cstrin ...
- [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, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- Pascal 语言中的关键字及保留字
absolute //指令(变量) abstract //指令(方法) and //运算符(布尔) array //类型 as //运算符(RTTI) asm //语句 assembler //向后兼 ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 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 ...
- caffe学习笔记(一),ubuntu14.04+GPU (用Pascal VOC2007训练数据,并测试)
把源代码跑起来了,将实验过程记录如下,用于新手入门. 今天和师兄师姐才跑通,来分享下心得.(预训练网络:ImageNet_model,训练集:PASCAL VOC2007, GPU) 首先,整个tra ...
随机推荐
- mount 挂载
mount 挂载出现 这是咋回事.找了找度娘,说是磁盘没有格式化.好吧,mkfs ext4 /dev/sda4 ,提示 没有有效的快给格式化,好奇怪啊,昨天明明分号区了,我记错了. fdisk看一下, ...
- Python 封装一个函数,查找文字字符串数字英文下标
def abc(str,data): count = [] numMax = 0 for a in range(len(str)): if a == 0: temp = str.find(data, ...
- 关于第一次web前端面试的记录
最近参加了一场面试,感觉自己题目都懂,但是说起来就是有点说不明白,所以写个博客整理以下吧.答案不少不是面试时回答的答案,只是整理一下可行答案 1. 如图1,使B相对于A垂直居中 图1 <styl ...
- thinkphp 动态配置
之前的方式都是通过预先定义配置文件的方式,而在具体的操作方法里面,我们仍然可以对某些参数进行动态配置(或者增加新的配置),主要是指那些还没有被使用的参数. 设置新的值: C('参数名称','新的参数值 ...
- (转)OC学习笔记 @property的属性 strong 和 weak 理解
在ObjectiveC里,用@property访问所有的实例变量.@property有一对属性:strong 和 weak.官方文档里的解释晦涩难懂:Stack Overflow里的用户RDC (ht ...
- 牛客多校第十场 H Stammering Chemists 判断图同构
题意: 给出一个无向图,表示一种有机物质的结构式,问你这个有机物质是列表中的哪个. 题解: 判断图同构需要枚举全排列以对应点,但是此题中几乎只需要将点度数排序后一个一个比较,对于甲基位置再加个特判即可 ...
- 使用nginx访问本地电脑的目录文件
cat /usr/local/opt/nginx/ //nginx路径 cd /usr/local/opt/nginx/html //localhost的指向 ln -s ~/Documents do ...
- 3步永久性激活IntelliJ IDEA 亲测有效
1.进到文件夹中:C:\Windows\System32\drivers\etc ,找到hosts文件,用文本编辑器打开文件,将“ 0.0.0.0 account.jetbrains.com ”添加 ...
- jquery下拉框应用
<!DOCTYPE html> <html lang="en"> <head> <script src="http://code ...
- docker使用gitlab持续集成(1)
修改ssh连接端口vi /etc/ssh/sshd_config 写docker-compose.yml文件配置gitlab version: '3' services: gitlab: image: ...