当前位置:网站首页>A.前缀极差(C语言)
A.前缀极差(C语言)
2022-06-11 03:38:00 【洛907】
一、题目
蒜头君有 nnn 个数,他提出了 qqq 个问题,每个问题是说,询问前 xxx 个数的极差(最大值减最小值)。你能帮助他解决这 qqq 个问题吗?
Input
第一行两个整数 n,q(1≤n,q≤105)n, q(1 \leq n, q \leq 10 ^ 5)n,q(1≤n,q≤105)
第二行 nnn 个整数 ai(1≤ai≤109)a_i(1 \leq a_i \leq 10 ^ 9)ai(1≤ai≤109)
表示蒜头君的 nnn 个数第三行 qqq 个整数 xi(1≤xi≤n)x_i(1 \leq x_i \leq n)xi(1≤xi≤n) ,表示每一次询问
Output
输出一行,包含 qqq 个整数,表示每一次询问的答案
Sample 1
Inputcopy Outputcopy
5 5
3 2 4 5 1
1 2 3 4 5
0 1 2 3 4
二、解决方案
1.思路
①这道题根据题意,我们要计算先x项的极差,那我们就要知道前x项的最大值与最小值,在之前写的代码中,我在for循环里每一次计算前x项的极差,最后的结果是超时的。
②所以为了更简化我们的代码,我们再输入数字的时候就提前把它前x项的最大值与最小值计算出来,后面直接用即可,就避免了重复的运算
——————————————
2.代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int n=0,q=0;
scanf("%d %d",&n,&q);
int arr[100005]={
0};
int max[100005]={
0},min[100005]={
0};
for(int i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
if(i==1)
{
max[i]=arr[i],min[i]=arr[i];
}
else
{
max[i] = max[i-1]>arr[i]?max[i-1]:arr[i];
min[i] = min[i-1]<arr[i]?min[i-1]:arr[i];
}
}
int x=0;
for(int i=0;i<q;i++)
{
scanf("%d",&x);
printf("%d ",max[x]-min[x]);
}
return 0;
}
边栏推荐
- Course classification tree structure display
- [数据集]|无人机视角
- Matlab reports an error when trying to use * * * as a function problem, and tries to execute script PCA as a function:
- RHEL7 切换字符编码为GBK
- OPENSSL ASN. 1, DER, PEM, X509
- Sentence s, paragraph P in VIM text object
- Docker builds a redis Cluster - three machines, three masters and three slaves
- [CNN]|CNN与Transformer区别
- 基于SSM框架的学生老师考试管理系统
- From function test to advanced automation test, I stayed up 7 days to sort out this 3000 word super complete learning guide [with network disk resources]
猜你喜欢

OpenGL错误指南

Solution to the problem of gd32f4 serial port DMA reception

618 coming! Can oppo reno6, which is sold through all channels with high price and low configuration, win?

Student teacher examination management system based on SSM framework

代码复现CSRF攻击并解决它

OpenGL Chapter 10 illuminant

Docker swarm installs redis cluster (bitnami/redis cluster:latest)

SQL | some indicators of the game industry

Free flying animation of paper plane based on SVG

Xu Li 618, how can Suning fight this hard battle?
随机推荐
Samsung Galaxy S21 ultra and Apple iPhone 13 Pro Max: which one should you choose
SQL query users logged in for three consecutive days
Interface performance optimization ideas
rt-thread测试
Lianyirong (passed)
【可解释】|深层网络的公理化属性(Axiomatic Attribution for Deep Networks)
VNC remote configuration of Galaxy Kirin server system
[elt.zip] openharmony paper Club - electronic device software update compression
JS the most commonly used sorting - hand tearing JS series
【SignalR全套系列】之在.Net6中实SignalR通信
OpenGL第十一章 多光源
RHEL7 切换字符编码为GBK
Xu Li 618, how can Suning fight this hard battle?
UML series articles (28) architecture modeling - collaboration
ARM开发板方案与厂商分析
基于SSM框架的连锁超市购物零售后台管理系统
[cnn]|differences between CNN and transformer
[CNN]|平移不变性
右键 powershell here 功能添加
如何提高回归测试效率