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 ...
随机推荐
- JS window对象取消计时器clearInterval() clearInterval() 方法可取消由 setInterval() 设置的交互时间。
取消计时器clearInterval() clearInterval() 方法可取消由 setInterval() 设置的交互时间. 语法: clearInterval(id_of_setInterv ...
- margin与padding
1.不加内边距的div: <div style="width:150px; height:150px; "> <div style="width: ...
- 打开桌面上的图标就会弹出"打开些文件可能会对您的计算机有害"解决方案
问题截图 方案步骤 运行 gpedit.msc 用户配置--管理模板--windows组件--附件管理器 找到中等危险文件类型抱含列表后右键-编辑 在指定中等风险扩展名中加入你文件的扩展名 应用, 确 ...
- BlockingQueu 阻塞队列
java.util.concurrent public interface BlockingQueue<E> extends Queue<E> 简介 当阻塞队列插入数据时: 如 ...
- UIWindow & UIWindowLevel笔记
一.UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面.UIWindow在程序中主要起到三个作用: 1.作为容器 ...
- ACM-ICPC 2018 沈阳赛区网络预赛-B,F,G
学长写的 F. Fantastic Graph "Oh, There is a bipartite graph.""Make it Fantastic." X ...
- Notepad++如何对比文件 Notepad++对比两个文件代码方法
大家在使用Notepad++的时候,需要对编辑的两个文件进行比较,找出两个文件代码的区别,快速进行编辑修改,那么Notepad++如何对比文件,下面小编就给大家带来Notepad++对比两个文件代码方 ...
- sql 生成javabean实体
select a.name,c.name,b.name,'private String '+lower(c.name)+';' from sysobjects a, systypes b, sysco ...
- Memory barrier,
A memory barrier, also known as a membar, memory fence or fence instruction, 是一种屏障指令,它使中央处理单元(CPU)或编 ...
- this.$router.go()和this.$router.push()的差别
1.this.$router.go(val) => 在history记录中前进或者后退val步,当val为0时刷新当前页面. 2.this.$router.push(path) => 在h ...