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 ...
随机推荐
- linux下设置Git
目录 ## Git介绍 1.工作原理 2.SVN与Git的最主要的区别? 3.操作 4.创建本地仓库 5.把文件添加到本地仓库 6.版本回退 7.理解工作区(workspace)与暂存区(index) ...
- 关于print()里面的sep和end参数的使用
print('hello', 'world') #默认用空格隔开 #hello world print('hello', 'world', sep='wuli') #sep=''可以用来设置连接的字符 ...
- python中while与else的联姻
循环使用 else 语句在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断 ...
- 解决 php artisan route:list 报错oauth-private.key文件不存在或不可读的
进入项目根目录命令行执行 php artisan passport:install 然后执行php artisan route:list,会提示 Class App\Http\Controllers\ ...
- photoshop钢笔工具简单记录
1. 移动锚点 Ctrl + 左键 2. 增加.删除锚点 左键(显示+.-) 3. 直线曲线相互转换 Alt + 左键(注意提示) 默认情况下为直线,按住Alt鼠标左键点击目标锚点,目标锚点两边的直线 ...
- vue组件添加鼠标滚动事件
在一个组件标签上加鼠标滚动事件,应该写成 @mousewheel.native
- NOIp2018集训test-9-2(pm)
其实这套题我爆0了,T1define 写成ddefine编译错误 T2有两个变量爆int 但是我看zwh不在悄悄地改了,我心里还是十分愧疚(没有)的.主要是林巨已经虐我125了要是再虐我200分我大概 ...
- Java-Class-@I:org.springframework.stereotype.Service
ylbtech-Java-Class-@I:org.springframework.stereotype.Service 1.返回顶部 2.返回顶部 1. package com.ylbtech. ...
- [转]C# 将类的内容写成JSON格式的字符串
将类的内容写入到JSON格式的字符串中 本例中建立了Person类,赋值后将类中内容写入到字符串中 运行本代码需要添加引用动态库Newtonsoft.Json 程序代码: using System; ...
- Spark 调优之ShuffleManager、Shuffle
Shuffle 概述 影响Spark性能的大BOSS就是shuffle,因为该环节包含了大量的磁盘IO.序列化.网络数据传输等操作. 因此,如果要让作业的性能更上一层楼,就有必要对 shuffle 过 ...