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 ...
随机推荐
- K-mean matlab 实现代码
一.K均值聚类算法 算法步骤如下: 1.初始化 已知数据集合X,及事先指定聚类的总类数N,在X中随机选取N个对象作为初始的聚类中心. 2.设定迭代终止条件 通常设置最大循环次数或者聚类中心的变化误差. ...
- 论文阅读笔记:《Interconnected Question Generation with Coreference Alignment and Conversion Flow Modeling》
论文阅读:<Interconnected Question Generation with Coreference Alignment and Conversion Flow Modeling& ...
- python_django_template_url反向解析
什么是url反向解析? 一般我们网址在diango内部匹配顺序为:网址→ url → views → templates → <a href="suck/good/"> ...
- Echart中X轴为时间坐标刻度时,后台返回时间List被强制转化为时间戳问题
if(recordlist!=null&&recordlist.size()>0) { for (Record record : recordlist) { //根据频次决定使用 ...
- Qt 【无法打开 xxxx头文件】
经过多次磕碰,终于发现了通用的办法. 测试环境Qt5.5.1 mvcs 比如需要用到QtWin 直接去包含然后运行,but fail, 我去查找他的父类 QtWinExtras Qt自带的自动补全, ...
- 阶乘质因子分解——lightoj1035
#include<bits/stdc++.h> using namespace std; #define ll long long #define maxn 200 int primes[ ...
- 关于rem单位的使用
rem在移动端应用可参考淘宝的页面http://m.taobao.com (html的font-size通过动态计算获取) 页面基准320px(20px),html font-size值的计算: 注: ...
- Delphi全面控制Windows任务栏
使用Windows95/NT/98操作系统的用户知道:Windows正常启动后,在电脑屏幕下方出现一块 任务栏.从系统功能角度而言,整个任务栏包括几个不同的子区域,从左至右依次是:开始 按钮.应用程序 ...
- 执行SQL语句---SELECT
1.通常从MySQL数据库中检索数据有4个步骤: (1)发出查询: 用mysql_query发出查询. (2)检索数据: 用mysql_store_result/mysql_use_result (3 ...
- 剑指offer——34之字打印二叉树
题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 题解: 与上道题没区别,就是在存入数据时 ...